日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

模擬斗地主和學(xué)生管理系統(tǒng) IO 版

 印度阿三17 2019-03-17

1、模擬斗地主

public class PlayCards {
    public static void main(String[] args) {

        String[] color = {"黑桃", "梅花", "方片", "紅桃"};
        String[] num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        ArrayList<String> cards = new ArrayList<>();

        //洗牌
        for (int i = 0; i < color.length; i  ) {
            for (int j = 0; j < num.length; j  ) {
                cards.add(color[i]   num[j]);
            }
        }
        cards.add("大王");
        cards.add("小王");

        Collections.shuffle(cards);

        ArrayList<String> 林志玲 = new ArrayList<>();
        ArrayList<String> 林心如 = new ArrayList<>();
        ArrayList<String> 舒淇 = new ArrayList<>();

        //留下3張底牌
        for (int i = 0; i < cards.size() - 3; i  ) {
            if (i % 3 == 0) {
                林志玲.add(cards.get(i));
            } else if (i % 3 == 1) {
                林心如.add(cards.get(i));
            } else if (i % 3 == 2) {
                舒淇.add(cards.get(i));
            }
        }

        System.out.println("林志玲:"   林志玲);
        System.out.println("林心如:"   林心如);
        System.out.println("舒淇:"   舒淇);

        System.out.println("底牌:");
        for (int i = cards.size() - 3; i < cards.size(); i  ) {
            System.out.println(cards.get(i));
        }
    }
}

2、學(xué)生管理系統(tǒng) IO 版本

先在項目的根目錄下創(chuàng)建 student.txt,用戶存儲用戶輸入的信息。

學(xué)生類:

    public class Student {
        
        private String id;
        private String name;
        private String age;
        private String address;
        
        // 空參和有參構(gòu)造方法
        // getter、setter方法
        
    }


系統(tǒng)程序:

    public class StudentManagerIO {
        public static void main(String[] args) throws IOException {
    
            String fileName = "student.txt";
    
            //為了使主程序回到這里,使用循環(huán)
            while (true) {
                //學(xué)生管理系統(tǒng)主界面
                System.out.println("----------歡迎來到學(xué)生管理系統(tǒng)----------");
                System.out.println("1 查看所有學(xué)生");
                System.out.println("2 添加學(xué)生");
                System.out.println("3 刪除學(xué)生");
                System.out.println("4 修改學(xué)生");
                System.out.println("5 退出系統(tǒng)");
                System.out.println("請輸入你的選擇:");
    
                //創(chuàng)建鍵盤錄入對象
                Scanner scanner = new Scanner(System.in);
                String choiceString = scanner.nextLine();
    
                //使用 Switch語句
                switch (choiceString) {
                    case "1":
                        //查看所有學(xué)生
                        findAllStudent(fileName);
                        break;
                    case "2":
                        //添加學(xué)生
                        addStudent(fileName);
                        break;
                    case "3":
                        //刪除學(xué)生
                        deleteStudent(fileName);
                        break;
                    case "4":
                        //修改學(xué)生
                        updateStudent(fileName);
                        break;
                    case "5":
                        //退出
                        System.out.println("謝謝你的使用");
                        System.exit(0); //JVM退出
                    default:
                        System.out.println("你的輸入有誤,請重新選擇");
                }
            }
        }
    
        //從文件中讀取數(shù)據(jù)到集合
        private static void readData(String fileName, ArrayList<Student> array) throws IOException {
    
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
    
            String line;
            while ((line = reader.readLine()) != null) {
                String[] datas = line.split(",");
                Student student = new Student();
                student.setId(datas[0]);
                student.setName(datas[1]);
                student.setAddress(datas[2]);
                student.setAddress(datas[3]);
    
                array.add(student);
            }
    
            reader.close();
        }
    
        //把集合中的數(shù)據(jù)寫入文件
        private static void writeData(String fileName, ArrayList<Student> array) throws IOException {
    
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    
            for (int i = 0; i < array.size(); i  ) {
                Student student = array.get(i);
                StringBuffer sb = new StringBuffer();
                sb.append(student.getId()).append(",").append(student.getName()).append(",")
                        .append(student.getAge()).append(",").append(student.getAddress());
    
                writer.write(sb.toString());
                writer.newLine();
                writer.flush();
            }
    
            writer.close();
        }
    
        //修改學(xué)生
        public static void updateStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            readData(fileName, array);
    
            Scanner sc = new Scanner(System.in);
            System.out.println("請輸入你要修改的學(xué)生的學(xué)號:");
            String id = sc.nextLine();
    
            int index = -1;
            for (int i = 0; i < array.size(); i  ) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    index = i;
                    break;
                }
            }
    
            if (index == -1) {
                System.out.println("不好意思,你要修改的學(xué)號對應(yīng)的學(xué)生信息不存在,請回去重新你的選擇");
            } else {
                System.out.println("請輸入學(xué)生新姓名:");
                String name = sc.nextLine();
                System.out.println("請輸入學(xué)生新年齡:");
                String age = sc.nextLine();
                System.out.println("請輸入學(xué)生新居住地:");
                String address = sc.nextLine();
    
                Student student = new Student();
                student.setId(id);
                student.setName(name);
                student.setAge(age);
                student.setAddress(address);
    
                array.set(index, student);
                writeData(fileName, array);
                System.out.println("修改成功?。?!");
            }
        }
    
        //刪除學(xué)生
        public static void deleteStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            readData(fileName, array);
    
            Scanner scanner = new Scanner(System.in);
            System.out.println("請輸入需要刪除的學(xué)生學(xué)號:");
            String id = scanner.nextLine();
    
            int index = -1;
            for (int i = 0; i < array.size(); i  ) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    index = i;
                    break;
                }
            }
    
            if (index == -1) {
                System.out.println("不好意思,你要刪除的學(xué)生信息不存在,請重新選擇");
            } else {
                array.remove(index);
                writeData(fileName, array);
                System.out.println("刪除成功!!");
            }
        }
    
        //添加學(xué)生信息
        public static void addStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            //從文件中讀取數(shù)據(jù)到集合
            readData(fileName, array);
    
            Scanner scanner = new Scanner(System.in);
            String id;
    
            while (true) {
                System.out.println("請輸入學(xué)生學(xué)號:");
                id = scanner.nextLine();
                //判斷學(xué)號是否被占用
                boolean flag = isExist(array, id);
                if (flag) {
                    System.out.println("對不起,你輸入的學(xué)號被占用,請重新輸入");
                } else {
                    break;
                }
            }
    
            System.out.println("請輸入學(xué)生姓名:");
            String name = scanner.nextLine();
            System.out.println("請輸入學(xué)生年齡:");
            String age = scanner.nextLine();
            System.out.println("請輸入學(xué)生住址:");
            String address = scanner.nextLine();
    
            Student student = new Student();
            student.setId(id);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
    
            array.add(student);
            writeData(fileName, array);
            System.out.println("添加成功!!");
        }
    
        //判斷學(xué)號是否被占用
        private static boolean isExist(List<Student> array, String id) {
            for (int i = 0; i < array.size(); i  ) {
                Student student = array.get(i);
                if (student.getId().equals(id)) {
                    return true;
                }
            }
            return false;
        }
    
        //查看所有學(xué)生
        public static void findAllStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            readData(fileName, array);
    
            if (array.size() == 0) {
                System.out.println("不好意思,暫時沒有學(xué)生信息可供查詢,請重新選擇");
                return;
            }
    
            System.out.println("學(xué)號\t姓名\t年齡\t居住地");
            array.forEach(student ->
                    System.out.println(student.getId()   "\t"   student.getName()   "\t"  
                            student.getAddress()   "\t"   student.getAddress()
                    )
            );
        }
    }
來源:http://www./content-4-141651.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多