1:获取10个1-20之间的随机数,要求不能重复
import java.util.ArrayList;
import java.util.Random;
public class SuiJiShu {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> array = new ArrayList<Integer>();
int count = 0;
while (count < 10) {
int number = r.nextInt(20) + 1;
if (!array.contains(number)) {
array.add(number);
count++;
}
}
for (Integer i : array) {
System.out.println(i);
}
}
}
2.键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class MaxDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
while (true) {
int number = sc.nextInt();
if (number != 0) {
list.add(number);
} else {
break;
}
}
Integer[] i = new Integer[list.size()];
list.toArray(i);
Arrays.sort(i);
System.out.println("您输入的最大数为:" + i[i.length - 1]);
}
}