找出数组中最大和最小值
/*
数组获取最值(获取数组中的最大值最小值)
*/
public class ArrayTest2 {
public static void main(String[] args) {
int[] arr = {123, 451, 45, 12, 556, 12, 412};
//1、默认将第一个元素作为最大值以及最小值
int maxNumber = arr[0];
int minNumber = arr[0];
//2、从第二个元素开始循环依次比较
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxNumber) {
maxNumber = arr[i];
} else if (arr[i] < minNumber) {
minNumber = arr[i];
}
}
System.out.println("数组最大值为:" + maxNumber);
System.out.println("数组最小值为:" + minNumber);
}
}
标签:maxNumber,arr,minNumber,int,找出,最小值,数组 From: https://www.cnblogs.com/k8409/p/18333263