import java.util.Random;public class RandomArray { /** * 使用循环生成随机生成10个0--99的随机数并保存至数组中。再找出数组中最大值和最小值,输出到控制台台 */ public static void main(String[] args) { Random rnd = new Random(); int[] ns = new int[10]; for (int i = 0; i < 10; i++) { ns[i] = rnd.nextInt(100); } int min = ns[0], max = ns[0]; for (int j = 1; j < ns.length; j++) { if(ns[j]>max) max = ns[j]; if(ns[j]<min) min = ns[j]; } System.out.println(max); System.out.println(min); } }
直接先用 Arrays.Sort 排序后 更好啊
LZ以前学BASIC的?Java的数组访问是用中括号的。
那个array.sort排序怎么给报错呢?说是这个错误....The method Sort(int[]) is undefined for the type Array
import java.util.Random;public class RandomArray {
/**
* 使用循环生成随机生成10个0--99的随机数并保存至数组中。再找出数组中最大值和最小值,输出到控制台台
*/
public static void main(String[] args) {
Random rnd = new Random();
int[] ns = new int[10];
for (int i = 0; i < 10; i++) {
ns[i] = rnd.nextInt(100);
} int min = ns[0], max = ns[0];
for (int j = 1; j < ns.length; j++) {
if(ns[j]>max) max = ns[j];
if(ns[j]<min) min = ns[j];
}
System.out.println(max);
System.out.println(min);
}
}