类名 ArrayMaximum
类代码
import java.util.Arrays;
public class ArrayMaximum {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("用增强型for循环找出最大的那个数");
int[] a = new int[10];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
}
System.out.println(Arrays.toString(a));
int max = a[a.length - 1];
for (int each : a) {
if (max < each) {
max = each;
}
}
System.out.println("最大的那个数:" + max);
}
}
运行结果
用增强型for循环找出最大的那个数
[44, 28, 20, 25, 23, 60, 1, 94, 64, 76]
最大的那个数:94