1.生成0-9的20个随机数,将20个随机数放入集合中,手动输入0-9的个位数,将集合中的出现的这个个位数放到集合最前面,优先输出。
package homework4;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Four {
public static void main(String[] args) {
LinkedList<Integer> all = new LinkedList<Integer>();
Random random = new Random();
int num = random.nextInt(100);//生成随机数0-100
for (int i = 0; i < 20; i++) { //生成20个随机数
num = (int) (Math.random() * 100);
if (num != 0) {
all.add(num);
}
}
System.out.println("随机生成的20个数为:");
for (int i = 0; i < all.size(); i++) {
}
System.out.println(all);
System.out.println("\n"+"判断手动输入0-9的个位数是否存在于集合中:");
System.out.print("请输入数字:");
Scanner src = new Scanner(System.in);
int input = src.nextInt();
for (int i = 0; i < all.size(); i++) {
if(input == all.get(i)){
//System.out.println("将出现的个位数放到集合的最前面!");
all.remove(i);
all.addFirst(input);
}
}
System.out.print(all+" ");
}
}