class Array {
public static void main(String[] args) {
int[] arr ={6,2,9,15,1,5,20,7,186,2,9,15,1,5,20,7,18};
int max = getMax(arr);
System.out.println(max);
int min =getMin(arr);
System.out.println(min);
}
//得到最大值
public static int getMax(int[] arr) {
int max =arr[0];
for (int i =0;i < arr.length ;i++ ) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
//得到最小值
public static int getMin(int[] arr) {
int min =arr[0];
for (int i=0;i <arr.length ;i++ ) {
if (min > arr[i]) {
min = arr[i];
}
}
return min;
}
} |
|