最强黑马程序员_B站IO项目之随机点名_所有题目解答

最后一题没弄懂也不想弄懂了

制造假数据

姓:https://hanyu.baidu.com/s?wd=%E7%99%BE%E5%AE%B6%E5%A7%93&device=pc&from=home

男名:http://www.haoming8.cn/baobao/10881.html
女名:http://www.haoming8.cn/baobao/7641.html

package Project01;  
  
import cn.hutool.core.util.ReUtil;  
import cn.hutool.http.HttpUtil;  
  
import java.util.*;  
import java.util.stream.Collectors;  
  
public class demo01 {  
    public static <R> void main(String[] args) {  
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";  
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";  
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";  
		//获取 html 格式
        String familyNameStr = HttpUtil.get(familyNameNet);  
        String boyNameStr = HttpUtil.get(boyNameNet);  
        String girlNameStr = HttpUtil.get(girlNameNet);  
		//对html 格式进行正则化 获取想要的数据
        List<String> familyNameTempList = ReUtil.findAll("(.{4})(,|。)", familyNameStr, 1);  
        List<String> boyNameTempList =  ReUtil.findAll("([\u4E00-\u9FA5]{2})(、|。)", boyNameStr, 1);  
        List<String> girlNameTemplist = ReUtil.findAll("(.. ){4}..", girlNameStr, 0);  
  
        //System.out.println(familyNameTempList);  
  
        ArrayList<String> boyNameList = new ArrayList<>();  
		//去重男孩姓名
        for (String s : boyNameTempList) {  
            if(!boyNameList.contains(s)){  
                boyNameList.add(s);  
            }  
        }  
        //得到一个完整的男孩名字数组  
        System.out.println(boyNameList);  
  
       // System.out.println(girlNameTemplist);  
		  //将姓氏进行分割
        List<String> familyNameList = familyNameTempList  
                .stream()  
                .flatMap(s -> Arrays.stream(s.split("")))  
                .collect(Collectors.toList());  
        //完整的姓氏数组  
        System.out.println(familyNameList);  
		  //对女孩姓名进行分割
        ArrayList<String> girlNameList = new ArrayList<>();  
        for (String s : girlNameTemplist) {  
            String[] s1 = s.split(" ");  
  
            for (String s2 : s1) {  
                girlNameList.add(s2);  
            }  
        }  
        //得到一个完整的女孩名字数组  
        System.out.println(girlNameList);  
  
  
    }  
}

随机点名器

第一题

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第1张图片

// shuffle 一下 即可获得随机姓名

第二题

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第2张图片

 public static void main(String[] args) throws IOException {  
        //1. 读取文件  
        BufferedReader fr = new BufferedReader(new FileReader("D:\\test.txt"));  
        ArrayList<Student> boy = new ArrayList<>();  
        ArrayList<Student> girl = new ArrayList<>();  
  
        String str;  
        //创建了2个列表,存放男和女  
        while((str = fr.readLine())  != null){  
            String[] split = str.split("-");  
            if(split.length == 3){  
                String name = split[0];  
                String gender = split[1];  
                int age = Integer.parseInt(split[2]);  
                Student student = new Student(name, gender, age);  
  
                if("男".equals(gender)){  
                    boy.add(student);  
                }else  
                    girl.add(student);  
            }  
  
  
  
        }  
  
//        System.out.println(boy);  
//        System.out.println(girl);  
        //创建随机数  
        /*  
        1. 如果少于0.7 则是70%概率抽取到男性  
        2. 否则是女性  
         */        Random random = new Random();  
  
        ArrayList<Integer> n = new ArrayList<>();  
  
        for (int i = 0; i < 100; i++) {  
            float v = random.nextFloat();  
            if(v <= 0.7){  
                Collections.shuffle(boy);  
                //System.out.println(boy.get(0));  
                n.add(1);  
            }else {  
                Collections.shuffle(girl);  
                //System.out.println(girl.get(0));  
                n.add(0);  
            }  
        }  
        int x = 0; //代表男性概率  
        int y = 0; //代表女性概率  
        for (int i = 0; i < n.size(); i++) {  
            if(n.get(i)== 1){  
                x++;  
            }else  
                y++;  
        }  
  
        System.out.println((float) x/100);  
        System.out.println((float) y/100);  
        fr.close();  
  
    }

最后的结果显示:
抽取到男生的概率为0.67
抽取到女生的概率为0.33

第三题

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第3张图片

public static void main(String[] args) throws IOException {  
    BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));  
  
    String line = null;  
  
    ArrayList<Student> students = new ArrayList<>();  
  
    while ((line = br.readLine()) != null) {  
        String[] split = line.split("-");  
        if (split.length == 3) {  
            String name = split[0];  
            String gender = split[1];  
            int age = Integer.parseInt(split[2]);  
            Student student = new Student(name, gender, age);  
            students.add(student);  
        }  
    }  
  
    for (int i = 0; i < students.size(); i++) {  
        if( i == 2){  
            for (Student student : students) {  
                if("张三".equals(student.getName())){  
                    System.out.println(student);  
                }  
  
            }  
        }else{  
            Collections.shuffle(students);  
            System.out.println(students.get(0));  
        }  
  
    }  
        br.close();  
    }

第四题

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第4张图片

思路:不论有多少次数,只要点完名就会开始下一轮,创建一个temp和students,如果temp空了,就把students全部导入到temp中

public static void main(String[] args) throws IOException {  
    BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));  
  
    String line = null;  
  
    ArrayList<Student> students = new ArrayList<>();  
  
    ArrayList<Student> temp = new ArrayList<>();  
  
    while ((line = br.readLine()) != null) {  
        String[] split = line.split("-");  
        if (split.length == 3) {  
            String name = split[0];  
            String gender = split[1];  
            int age = Integer.parseInt(split[2]);  
            Student student = new Student(name, gender, age);  
            students.add(student);  
        }  
    }  
  
    for (int i = 0; i < 100; i++) {  
        if (temp.isEmpty()){  
            temp.addAll(students);  
        }  
  
        Collections.shuffle(temp);  
        System.out.println(temp.get(0));  
        temp.remove(0);  
  
    }  
}

第五题

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第5张图片
权重

最强黑马程序员_B站IO项目之随机点名_所有题目解答_第6张图片

使用0~1的每一个点
最强黑马程序员_B站IO项目之随机点名_所有题目解答_第7张图片

package project.随机点名;  
  
import java.io.*;  
import java.util.ArrayList;  
import java.util.Arrays;  
import java.util.Collections;  
import java.util.Random;  
  
public class demo05 {  
    public static void main(String[] args) throws IOException {  
        BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));  
          
        String line = null;  
  
        ArrayList<Student01> s01 = new ArrayList<>();  
        while((line = br.readLine()) != null){  
            String[] split = line.split("-");  
            String name = split[0];  
            String gender = split[1];  
            String age = split[2];  
            String weight = split[3];  
            int age_01 = Integer.parseInt(age);  
            double weight_01 = Double.parseDouble(weight);  
            Student01 s = new Student01(name, gender, age_01, weight_01);  
            s01.add(s);  
        }  
  
  
  
        br.close();  
  
  
        ArrayList<Double> WeightRange = new ArrayList<>();  
        double all_weight = 0.0;  
        //1. 计算权重的总和  
        for (Student01 stu : s01) {  
            all_weight += stu.getWeight();  
        }  
  
        for (Student01 stu : s01) {  
            WeightRange.add(stu.getWeight() / all_weight);  
        }  
  
        System.out.println(WeightRange);  
        //计算出每一个权重的范围  
        for (int i = 1; i < WeightRange.size(); i++) {  
            WeightRange.set(i,WeightRange.get(i) + WeightRange.get(i-1));  
        }  
  
        Random random = new Random();  
        double RandomNumber = random.nextFloat(1);  
  
        System.out.println(RandomNumber);  
        //出现了随机数字,然后使用二分查找这个索引  
        /*  
        二分查找法:  
        返回: -插入点 — 1         */        System.out.println(RandomNumber);  
        System.out.println(WeightRange);  
  
        int i = - (Collections.binarySearch(WeightRange, RandomNumber) + 1 ) ;  
  
        System.out.println(i);  
  
        System.out.println(s01.get(i));  
  
        s01.get(i).setWeight( s01.get(i).getWeight() / 2);  
  
        System.out.println(s01);  
  
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\test.txt"));  
  
        for (Student01 s : s01) {  
            bw.write(s.toString());  
            bw.newLine();  
        }  
  
        bw.close();  
    }  
}

数据集:
孙隽可-男-18-1
家森锦-男-21-1
宗安筠-女-26-1
水浩艺-男-21-1
欧亦凌-女-21-1
鄂铭宸-男-19-1
阳蓉蓉-女-24-1
阮高洁-女-21-1
佘希哲-男-24-1
良烟甜-女-20-1
容皓楠-男-18-1
拓冉芬-女-21-1
隆宇昕-男-25-1
良凡真-女-19-1
乔凌菡-女-22-1
闻梦菡-女-23-1
糜博涛-男-18-1
单云溪-女-18-1
伯熠泓-男-22-1
周鹏天-男-22-1

写的代码有点乱,后面没有整理,最近事情太多了(理由)。

你可能感兴趣的:(Java基础知识,java)