黑马程序员技术交流社区
标题:
基础测试题目里面的求最大值和最小值
[打印本页]
作者:
向天宣战
时间:
2015-6-10 21:52
标题:
基础测试题目里面的求最大值和最小值
/**
* 编写程序接收键盘输入的5个数,装入一个数组,并找出其最大数和最小数。
*
* @author zyy
*
*/
public class Test6 {
public static void main(String[] args) {
// 声明一个存储五个数字的整数数组
System.out.println("请输入五个整数(以回车符作为一个整数的结束):");
Integer[] console = new Integer[5];
for (int i = 0; i < console.length; i++) {
console[i] = getConsole();
}
System.out.println("\n数组中最大值是:" + getMaxValue(console) + "\n数组中最小值是:"
+ getMinValue(console));
}
/**
* 获取整数数组里面的最小值
*
* @param console
*/
private static Integer getMinValue(Integer[] console) {
if (null == console) {
throw new IllegalArgumentException("console is not allowed null");
}
int min = console[0];
for (int i = 1; i < console.length; i++) {
if (console[i] < min) {
min = console[i];
}
}
return min;
}
/**
* 获取整数数组里面的最大值
*
* @param console
*/
private static Integer getMaxValue(Integer[] console) {
if (null == console) {
throw new IllegalArgumentException("console is not allowed null");
}
int max = console[0];
for (int i = 1; i < console.length; i++) {
if (console[i] > max) {
max = console[i];
}
}
return max;
}
/**
* 获取控制台输入的整数,这部分没有进行异常处理,只考虑了安全的情况下
*
* @return 返回控制台获取的整数
*/
public static Integer getConsole() {
return new Scanner(System.in).nextInt();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2