本次文章讲述点名器程序与用户登录系统(均实现io流) 点名器为两道题 分别有不同的需求 用户名登录一题(共三题)
需求1: 一个文件里面存储了班级同学的姓名,每一个姓名占一行。 要求通过程序实现随机点名器。 运行结果要求: 被点到的学生不会再被点到。 但是如果班级中所有的学生都点完了, 需要重新开启第二轮点名。 核心思想: 点一个删一个,把删除的备份,全部点完时还原数据。
public static void main(String[] args) throws IOException { //1.定义变量,表示初始文件路径,文件中存储所有的学生信息 String src = "myiotest\\src\\com\\itheima\\myiotest5\\names.txt"; //2.定义变量,表示备份文件,一开始文件为空 String backups = "myiotest\\src\\com\\itheima\\myiotest5\\backups.txt"; //3.读取初始文件中的数据,并把学生信息添加到集合当中 ArrayList<String> list = readFile(src); //4.判断集合中是否有数据 if (list.size() == 0) { //5.如果没有数据,表示所有学生已经点完,从backups.txt中还原数据即可 //还原数据需要以下步骤: //5.1 读取备份文件中所有的数据 list = readFile(backups); //5.2 把所有的数据写到初始文件中 writeFile(src, list, false); //5.3 删除备份文件 new File(backups).delete(); } //5.集合中有数据,表示还没有点完,点一个删一个,把删除的备份到backups.txt当中 //打乱集合 Collections.shuffle(list); //获取0索引的学生信息并删除 String stuInfo = list.remove(0); //打印随机到的学生信息 System.out.println("当前被点到的学生为:" + stuInfo); //把删除之后的所有学生信息,写到初始文件中 writeFile(src, list, false); //把删除的学生信息备份(追加写入) writeFile(backups, stuInfo, true); } private static void writeFile(String pathFile, ArrayList<String> list, boolean isAppend) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(pathFile, isAppend)); for (String str : list) { bw.write(str); bw.newLine(); } bw.close(); } private static void writeFile(String pathFile, String str, boolean isAppend) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(pathFile, isAppend)); bw.write(str); bw.newLine(); bw.close(); } private static ArrayList<String> readFile(String pathFile) throws IOException { ArrayList<String> list = new ArrayList<>(); BufferedReader br = new BufferedReader(new FileReader(pathFile)); String line; while ((line = br.readLine()) != null) { list.add(line); } br.close(); return list; }
需求2:加入权重 使得点名更加公平 当某个学生被点中时他的权重就会减半 例如开始为10个人 每个人权重为1 则点名之后他的权重变为0.5
/先把学生信息拿出来 ArrayList<Student> list = new ArrayList<>(); BufferedReader br = new BufferedReader(new FileReader("D:\\io\\src\\myiotest\\myiotest6\\names.txt")); String line; while ((line = br.readLine()) != null){ String[] split = line.split("-"); Student stu = new Student(split[0],split[1],Integer.parseInt(split[2]),Double.parseDouble(split[3])); list.add(stu); } br.close(); //计算权重总和 double weight = 0; for (Student student : list) { weight = weight + student.getWeight(); } //计算每个人占的权重比例 double[] arr = new double[list.size()]; int index = 0; for (Student student : list) { arr[index] = student.getWeight() / weight; index++; } //计算每个人权重的范围 for (int i = 1; i < arr.length; i++) { arr[i] = arr[i] + arr[i-1]; } //随机抽取 //获取一个0.0- 1.0 之间的随机数 Double number = Math.random(); //方法为math自带的 作用为获取一个0-1的随机数 //通过二分查找法 找出随机数在那个数组中 int result = -Arrays.binarySearch(arr,number) - 1; //result就为找出来 在那个范围内的索引 //arrays的二分查找法 参数肯定是 要找的数组跟 那个值呀 Student stu = list.get(result); System.out.println(stu); //修改当前这个人的权重 double w = stu.getWeight() / 2; stu.setWeight(w); //把集合数据再次写到文件中去 BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\io\\src\\myiotest\\myiotest6\\names.txt")); for (Student student : list) { bw.write(student.toString()); bw.newLine(); } bw.close(); }
需求:写一个登陆小案例(添加锁定账号功能) 步骤: 将正确的用户名和密码手动保存在本地的userinfo.txt文件中。 保存格式为:username=zhangsan&password=123&count=0 让用户键盘录入用户名和密码 比较用户录入的和正确的用户名密码是否一致 如果一致则打印登陆成功 如果不一致则打印登陆失败,连续输错三次被锁定
//1.读取正确的用户名和密码 BufferedReader br = new BufferedReader(new FileReader("myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt")); String line = br.readLine();//username=zhangsan&password=123&count=0 br.close(); String[] userInfo = line.split("&"); String[] arr1 = userInfo[0].split("="); String[] arr2 = userInfo[1].split("="); String[] arr3 = userInfo[2].split("="); String rightUsername = arr1[1]; String rightPassword = arr2[1]; //count:表示用户连续输错的次数 int count = Integer.parseInt(arr3[1]); //2.用户键盘录入用户名和密码 Scanner sc = new Scanner(System.in); System.out.println("请输入用户名"); String username = sc.nextLine(); System.out.println("请输入密码"); String password = sc.nextLine(); //3.比较 if (rightUsername.equals(username) && rightPassword.equals(password) && count < 3) { System.out.println("登陆成功"); writeInfo("username=" + rightUsername + "&password=" + rightPassword + "&count=0"); } else { count++; if (count < 3) { System.out.println("登陆失败,还剩下" + (3 - count) + "次机会"); } else { System.out.println("用户账户被锁定"); } writeInfo("username=" + rightUsername + "&password=" + rightPassword + "&count=" + count); } } /* * 作用: * 写出一个字符串到本地文件中 * 参数: * 要写出的字符串 * */ public static void writeInfo(String content) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("myiotest\\src\\com\\itheima\\myiotest8\\userinfo.txt")); bw.write(content); bw.close(); }