ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

模拟斗地主和学生管理系统 IO 版

2019-03-17 11:39:50  阅读:254  来源: 互联网

标签:IO 管理系统 斗地主 System String student println array out


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、学生管理系统 IO 版本

先在项目的根目录下创建 student.txt,用户存储用户输入的信息。

学生类:

    public class Student {
        
        private String id;
        private String name;
        private String age;
        private String address;
        
        // 空参和有参构造方法
        // getter、setter方法
        
    }


系统程序:

    public class StudentManagerIO {
        public static void main(String[] args) throws IOException {
    
            String fileName = "student.txt";
    
            //为了使主程序回到这里,使用循环
            while (true) {
                //学生管理系统主界面
                System.out.println("----------欢迎来到学生管理系统----------");
                System.out.println("1 查看所有学生");
                System.out.println("2 添加学生");
                System.out.println("3 删除学生");
                System.out.println("4 修改学生");
                System.out.println("5 退出系统");
                System.out.println("请输入你的选择:");
    
                //创建键盘录入对象
                Scanner scanner = new Scanner(System.in);
                String choiceString = scanner.nextLine();
    
                //使用 Switch语句
                switch (choiceString) {
                    case "1":
                        //查看所有学生
                        findAllStudent(fileName);
                        break;
                    case "2":
                        //添加学生
                        addStudent(fileName);
                        break;
                    case "3":
                        //删除学生
                        deleteStudent(fileName);
                        break;
                    case "4":
                        //修改学生
                        updateStudent(fileName);
                        break;
                    case "5":
                        //退出
                        System.out.println("谢谢你的使用");
                        System.exit(0); //JVM退出
                    default:
                        System.out.println("你的输入有误,请重新选择");
                }
            }
        }
    
        //从文件中读取数据到集合
        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();
        }
    
        //把集合中的数据写入文件
        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();
        }
    
        //修改学生
        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("请输入你要修改的学生的学号:");
            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("不好意思,你要修改的学号对应的学生信息不存在,请回去重新你的选择");
            } else {
                System.out.println("请输入学生新姓名:");
                String name = sc.nextLine();
                System.out.println("请输入学生新年龄:");
                String age = sc.nextLine();
                System.out.println("请输入学生新居住地:");
                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("修改成功!!!");
            }
        }
    
        //删除学生
        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("请输入需要删除的学生学号:");
            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("不好意思,你要删除的学生信息不存在,请重新选择");
            } else {
                array.remove(index);
                writeData(fileName, array);
                System.out.println("删除成功!!");
            }
        }
    
        //添加学生信息
        public static void addStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            //从文件中读取数据到集合
            readData(fileName, array);
    
            Scanner scanner = new Scanner(System.in);
            String id;
    
            while (true) {
                System.out.println("请输入学生学号:");
                id = scanner.nextLine();
                //判断学号是否被占用
                boolean flag = isExist(array, id);
                if (flag) {
                    System.out.println("对不起,你输入的学号被占用,请重新输入");
                } else {
                    break;
                }
            }
    
            System.out.println("请输入学生姓名:");
            String name = scanner.nextLine();
            System.out.println("请输入学生年龄:");
            String age = scanner.nextLine();
            System.out.println("请输入学生住址:");
            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("添加成功!!");
        }
    
        //判断学号是否被占用
        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;
        }
    
        //查看所有学生
        public static void findAllStudent(String fileName) throws IOException {
    
            ArrayList<Student> array = new ArrayList<>();
            readData(fileName, array);
    
            if (array.size() == 0) {
                System.out.println("不好意思,暂时没有学生信息可供查询,请重新选择");
                return;
            }
    
            System.out.println("学号\t姓名\t年龄\t居住地");
            array.forEach(student ->
                    System.out.println(student.getId() + "\t" + student.getName() + "\t" +
                            student.getAddress() + "\t" + student.getAddress()
                    )
            );
        }
    }

标签:IO,管理系统,斗地主,System,String,student,println,array,out
来源: https://www.cnblogs.com/miantiao312/p/10546038.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有