ArrayList<String> boyNameList = new ArrayList<>(); ArrayList<String> girlNameList = new ArrayList<>(); //1.读取数据 BufferedReader br = new BufferedReader(new FileReader("D:\\java\\xuexi\\Java test\\name.txt")); //2.进行遍历 String ch; while ((ch = br.readLine()) != null) { String[] split = ch.split("-"); if (split[1].equals("男")) { //添加到集合中 boyNameList.add(ch); } else { girlNameList.add(ch); } } br.close(); //2.定义男女比例 ArrayList<Integer> list = new ArrayList<>(); Collections.addAll(list, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0); //3.定义变量,统计被点到的次数 int boyCount = 0; int girlCount = 0; Random r = new Random(); //4.循环100万次 for (int i = 0; i < 100000; i++) { //5.从权重集合中获取随机数据 int index = r.nextInt(list.size()); int weight = list.get(index); //6.判断获取的随机数据是1还是0 if (weight == 1) { Collections.shuffle(boyNameList); String boyInfo = boyNameList.get(0); System.out.println(boyInfo); boyCount++; } else if (weight == 0) { //打乱 Collections.shuffle(girlNameList); String girlInfo = girlNameList.get(0); System.out.println(girlInfo); girlCount++; } } System.out.println("随机抽取100万次,其中男生被抽到了" + boyCount); System.out.println("随机抽取100万次,其中女生被抽到了" + girlCount);