public class Test {
public static void main(String[] args) {
double []d={9.8,12,45,67,23,1.98,2.55,45};
double max=0;//当前最大值
double min=0;//当前最小值
for (int i = 0; i < d.length; i++) {
if(i==0) //假设第一个最大,最小
max=min=d[0];
if(d[i]>max) //大于最大
max=d[i];
if(d[i]
}
System.out.println("最大值:"+max);
System.out.println("最小值:"+min);
}
}
public class Test{
public static void main(String[] args) {
double[] arr = new double[]{9.8,12,45,67,23,1.98,2.55,45};
double max = arr[0];
double min = arr[0];
for(int i = 1;i
}
if(min>arr[i]){
min = arr[i];
}
}
System.out.println("最大值是"+max);
System.out.println("最小值是"+min);
}
}
public static void main(String[] args) {
int[] a = { 12, 4, 22, 3, 90, 316, 72 };
int max = a[0];
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
if (min > a[i]) {
min = a[i];
}
}
System.out.println(max);
System.out.println(min);
}
用Arrays.sort()先排序,再输出首尾元素就行!
啊,来晚了