Use Scanner, Random, Arraylist to complete a non-repetitive roll call program

Case: random roll call

First we use the Array class to convert the data into a collection

Let's first define an array to store the names

 List<String> list = Arrays.asList("zy","cg","dhh","zyx","zrx");

Create a collection through the construction method, let the elements in the list collection be taken out and stored in the ArrayList collection one by one

ArrayList<String> names = new ArrayList<>(list);

 Create scanner keyboard input and random numbers

//创建键盘输入
        Random random = new Random();
        //创建随机数
        Scanner scanner = new Scanner(System.in);

 Then use while to loop, judge the data input by the keyboard, randomly generate a value of the index, get the content corresponding to the index value, and then print out, use remove (remove) the random content to end the loop

 //循环
        while (true){
            System.out.println("点名请输入S/s");
            String input = scanner.next();//获取键盘输入的数据进行判断
            if (input.equals("S")||input.equals("s")){
                int index = random.nextInt(names.size()); //随机产生集合索引范围内的一个值
                //通过下标获取集合中的内容
                String name = names.get(index);
                //打印输出
                System.out.println("选中"+name+"啦!");
                //移除已随机过的内容
                names.remove(index);
            }else {
                System.out.println("输错了输错了,点不了名!!");
                break;//结束循环
            }

Finally, judge that if the collection is empty, end the loop

  if (names.size()==0){
                System.out.println("名都点完啦,点不了啦!!");
                break;
            }

The overall code is as follows:

public class Test01 {
    public static void main(String[] args) {
        //综合案例:使用Scanner、Random、Arraylist完成一个不重复的点名程序
        //使用Arrays类把数据转换成集合,这个是一个固定数组形式
        List<String> list = Arrays.asList("zy","cg","dhh","zyx","zrx");
        //通过构造方法创建名单集合:把list集合中元素一一取出存入names集合中
        ArrayList<String> names = new ArrayList<>(list);
        //创建键盘输入
        Random random = new Random();
        //创建随机数
        Scanner scanner = new Scanner(System.in);
        //循环
        while (true){
            System.out.println("点名请输入S/s");
            String input = scanner.next();//获取键盘输入的数据进行判断
            if (input.equals("S")||input.equals("s")){
                int index = random.nextInt(names.size()); //随机产生集合索引范围内的一个值
                //通过下标获取集合中的内容
                String name = names.get(index);
                //打印输出
                System.out.println("选中"+name+"啦!");
                //移除已随机过的内容
                names.remove(index);
            }else {
                System.out.println("输错了输错了,点不了名!!");
                break;//结束循环
            }
            //判断 如果集合为空,结束循环
            if (names.size()==0){
                System.out.println("名都点完啦,点不了啦!!");
                break;
            }
        }
    }
}

The result of the operation is as follows:

 

Guess you like

Origin blog.csdn.net/weixin_67224308/article/details/128059736