欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > IO练习--随机点名

IO练习--随机点名

2024/10/24 19:25:24 来源:https://blog.csdn.net/m0_46977298/article/details/141689332  浏览:    关键词:IO练习--随机点名
随机点名器1

需求:
有一个文件里面存储了班级同学的信息,每一个信息占一行。
格式为:张三-男-23
要求通过程序实现随机点名器。
运行效果:
第一次运行程序:随机同学姓名1(只显示名字)
第二次运行程序:随机同学姓名2(只显示名字)
第三次运行程序:随机同学姓名3(只显示名字) 

public class Tset {public static void main(String[] args) throws IOException {//把文件中的学生全部读取出来放到集合中ArrayList<String> list = new ArrayList<>();//使用缓冲流读,因为可以一次读取一行BufferedReader br = new BufferedReader(new FileReader("name.txt"));String info;while ((info = br.readLine()) != null) {list.add(info);}//随机选取一个元素并获取名字Collections.shuffle(list);String name = list.get(0).split("-")[0];//读取程序运行的次数FileInputStream fis = new FileInputStream("count.txt");int count = fis.read();count = count + 1;System.out.println("第" + (char)count +"次运行程序:" + name);//将程序的运行次数记录下来//当文件不存在时会先创建文件FileOutputStream fos = new FileOutputStream("count.txt");fos.write(count);}
}
随机点名器2--带概率的随机

需求:
一个文件里面存储了班级同学的信息,每一个学生信息占一行。
格式为:张三-男-23。
要求通过程序实现随机点名器
运行效果:
70%的概率随机到男生
30%的概率随机到女生
点击并拖拽以移动
总共随机100万次,统计结果,
注意观察:看生成男生和女生的比例是不是接近于7:3

public class Test02 {public static void main(String[] args) throws IOException {//首先把文件中的内容读取到集合中,男女分开存ArrayList<String> boyList = new ArrayList<>();ArrayList<String> girlList = new ArrayList<>();BufferedReader br = new BufferedReader(new FileReader("name.txt"));String info;while ((info = br.readLine()) != null) {String gender = info.split("-")[1];if (gender.equals("男")) {boyList.add(info);}if (gender.equals("女")) {girlList.add(info);}}//模拟概率ArrayList<Integer> plist = new ArrayList<>();Collections.addAll(plist, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);int boyCount = 0;int girlCount = 0;//循环一百万次for (int i = 0; i < 1000000; i++) {Collections.shuffle(plist);int number = plist.get(0);if (number == 1) {Collections.shuffle(boyList);String boyInfo = boyList.get(0);boyCount++;} else {Collections.shuffle(girlList);String girlInfo = girlList.get(0);girlCount++;}}//统计概率double p1 = (double) boyCount / 1000000;double p2 = (double) girlCount / 1000000;System.out.println(p1);System.out.println(p2);}
}

输出结果:

随机点名器3

需求:
一个文件里面存储了班级同学的姓名,每一个姓名占一行。
要求通过程序实现随机点名器。
第三次必定是张三同学
运行效果:
第一次运行程序:随机同学姓名1第二次运行程序:随机同学姓名2
第三次运行程序:张三

由于读取返回的是字符所对应的ASCII码点,无法直接与整数1,2...进行比较。

当有一个char类型的字符c,想要判断字符内容是否为2时,可这样比较:c == '2'。

代码如下:

public class Test03 {public static void main(String[] args) throws IOException {//把文件中的学生全部读取出来放到集合中ArrayList<String> list = new ArrayList<>();//使用缓冲流读,因为可以一次读取一行BufferedReader br = new BufferedReader(new FileReader("name.txt"));String info;while ((info = br.readLine()) != null) {list.add(info);}//读取程序运行的次数FileInputStream fis = new FileInputStream("count.txt");int count = fis.read();if (count == '2') {count = count + 1;System.out.println("第" + (char)count +"次运行程序:张三");} else {//随机选取一个元素并获取名字Collections.shuffle(list);String name = list.get(0).split("-")[0];count = count + 1;System.out.println("第" + (char)count +"次运行程序:" + name);}//将程序的运行次数记录下来//当文件不存在时会先创建文件FileOutputStream fos = new FileOutputStream("count.txt");fos.write(count);}
}

运行结果: 

随机点名器4--随机不重复

需求:
一个文件里面存储了班级同学的姓名,每一个姓名占一行,要求通过程序实现随机点名器。
运行效果:
被点到的学生不会再被点到
如果班级中所有的学生都点完了,需要自动的重新开启第二轮点名
细节1:假设班级有10个学生,每一轮中每个人只能被点到一次,程序运行10次,第一轮结束;

细节2:第11次运行的时候,我们自己不需要手动操作本地文件,要求程序自动开始第二轮点名。

首先来实现第一轮点名的代码:

保证抽过的不会再被抽到,可以用remove方法实现。

注意:

remove方法有两个,分别是根据索引和根据内容删除,内容的话方法的返回结果是布尔类型,而索引会把删除的元素进行返回。

        //一个班级里的10个学生ArrayList<String> list = new ArrayList<>();BufferedReader br = new BufferedReader(new FileReader("name_10.txt"));String info;while ((info = br.readLine()) != null) {list.add(info);}br.close();//随机选取一个元素并获取名字Collections.shuffle(list);String name = list.remove(0).split("-")[0];System.out.println(name);//更新文件中的名单BufferedWriter bw1 = new BufferedWriter(new FileWriter("name_10.txt"));for (String s : list) {bw1.write(s);bw1.newLine();}bw1.close();//将点到的人存到新的文件中BufferedWriter bw2 = new BufferedWriter(new FileWriter("callover.txt", true));bw2.write(name);bw2.newLine();bw2.close();

完整的代码:

当一轮点名结束时要恢复原来的名单。

public class Test04 {public static void main(String[] args) throws IOException {//首先读取文件中的内容ArrayList<String> list = readFile();//if (list.isEmpty())       {copyFile();list = readFile();String name = callOver(list);writeFile(name);} else {String name = callOver(list);writeFile(name);}}private static void writeFile(String name) throws IOException {//将点到的人存到新的文件中BufferedWriter bw2 = new BufferedWriter(new FileWriter("callover.txt", true));bw2.write(name);bw2.newLine();bw2.close();}private static String callOver(ArrayList<String> list) throws IOException {//随机选取一个元素并获取名字Collections.shuffle(list);String name = list.remove(0).split("-")[0];System.out.println(name);//更新文件中的名单BufferedWriter bw1 = new BufferedWriter(new FileWriter("name_10.txt"));for (String s : list) {bw1.write(s);bw1.newLine();}bw1.close();return name;}private static ArrayList<String> readFile() throws IOException {//一个班级里的10个学生ArrayList<String> list = new ArrayList<>();BufferedReader br = new BufferedReader(new FileReader("name_10.txt"));String info;while ((info = br.readLine()) != null) {list.add(info);}br.close();return list;}public static void copyFile() throws IOException {FileInputStream fis = new FileInputStream("callover.txt");FileOutputStream fos = new FileOutputStream("name_10.txt");int b;while ((b = fis.read()) != -1) {fos.write(b);}fos.close();fis.close();new File("callover.txt").delete();}
}

可以再修改一下: 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com