Java uses collections to make a random roll call system for students

package com.hepu.day0124;
import java.util.*;

/**
 * @Author: AxinGzZz
 * @Date: 2021/1/24 23:35
 */
public class Student {
    
    
    public void studentName() {
    
    
        //创建集合存入名字
        List<String> list = List.of("曹操", "荀彧", "许攸", "郭嘉", "杨修", "张辽", "乐进",
                "于禁", "张鸽", "徐晃", "典韦", "刘备", "关羽", "张飞", "赵云", "黄忠",
                "马超", "诸葛亮", "姜维", "刘禅", "庞统", "魏延", "关平", "黄月英",
                "孙权", "孙策", "周瑜", "陆逊", "太史慈", "大乔", "小乔");
        System.out.println("---------欢迎来到群英战吕布!共有"+list.size()+"名大将供你选择!---------");
        System.out.println("请选择出战人数!");
        List<String> list1 = new ArrayList<>();
        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        if (input < 1 || input > list.size()) {
    
    
            System.out.println("你输入的人数不对");
        } else {
    
    
            for (int i = 0; i < input; i++) {
    
    
                //获得随机下标
                int r = random.nextInt(list.size());
                //判断list1集合中是否有list集合中的元素
                boolean flag = list1.contains(list.get(r));
                if (flag == false) {
    
    
                    //将list集合的元素添加到list1集合
                    list1.add(list.get(r));
                } else {
    
    
                    i--;
                }
            }
        }
        //使用iterator迭代器遍历集合list1
        Iterator iterator = list1.iterator();
        System.out.println("恭喜以下大将迎战吕布:");
        while (iterator.hasNext()) {
    
    
            System.out.print(iterator.next() + " ");
        }
        System.out.println("\n");
        studentName();
    }
    //退出系统
    public void exit() {
    
    
        System.out.println("是否真的溜了?Y/N");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        if (str.equals("Y")) {
    
    
            System.out.println("已经成功跑路");
            System.exit(0);
        } else {
    
    
            System.out.println("迎战吕布");
            studentName();
        }
    }
    //选择界面
    public void design(){
    
    
        System.out.println("**********欢迎来到三国点名迎战吕布系统**********");
        System.out.println("1.绝不投降,跟吕布拼了");
        System.out.println("2.溜了,溜了干不过!");
        System.out.println("请选择:1.站着死 2.溜了,溜了");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        if (num == 1) {
    
    
            studentName();
        }
        if (num == 2) {
    
    
            exit();
        }
    }
}
package com.hepu.day0124;

/**
 * @Author: AxinGzZz
 * @Date: 2021/1/25 0:07
 */
public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Student student = new Student();
        student.design();
    }
}

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44193337/article/details/113101489