JAVA题目笔记(十八)集合综合练习(自动点名器项目)

一、自动点名器

(一)随机点名

要求:班里若干名学生,对学生进行随机点名

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
      //班里有N个同学,实现随机点名
        ArrayList<String> list=new ArrayList<>();
        Collections.addAll(list,"张三","张四","张五","张六","张七","张八","张九","张十");
        Random r=new Random();
        int count=0;
        while(count<10){
            int index=r.nextInt(8);
            System.out.println(list.get(index));
            count++;
        }
    }
}

(二)概率点名

要求:班里若干名学生,对学生进行随机点名,70%几率选择男生,30%几率选择女生

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
      //班里有N个同学,实现随机点名 男生70%概率  女生30%概率
        ArrayList<Student> list1=new ArrayList<>();
        ArrayList<Student> list2=new ArrayList<>();
        Student stu1=new Student("张1","男");
        Student stu2=new Student("张2","男");
        Student stu3=new Student("张3","男");
        Student stu4=new Student("张4","女");
        Student stu5=new Student("张5","女");
        Student stu6=new Student("张6","女");
        Student stu7=new Student("张7","男");
        Student stu8=new Student("张8","男");
        Student stu9=new Student("张9","男");
        Student stu10=new Student("张10","男");

        Collections.addAll(list1,stu1,stu2,stu3,stu7,stu8,stu9,stu10);
        Collections.addAll(list2,stu4,stu5,stu6);
        int count=0;
        Random r=new Random();
        while(count<10){
            int prob=r.nextInt(10)+1;
            System.out.print(prob);
            if(prob<=3){
                int index=r.nextInt(list2.size());
                System.out.println(list2.get(index).toString());
            }else {
                int index=r.nextInt(list1.size());
                System.out.println(list1.get(index).toString());
            }
            count++;
        }
    }
}

(三)不重复随机点名

要求:被点过的同学不会再次被点,当所有人都被点到后进行第二轮点名


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        //班里有N个同学,实现随机点名
        ArrayList<Student> list1=new ArrayList<>();
        ArrayList<Student> list2=new ArrayList<>();
        Student stu1=new Student("张1","男");
        Student stu2=new Student("张2","男");
        Student stu3=new Student("张3","男");
        Student stu4=new Student("张4","女");
        Student stu5=new Student("张5","女");
        Student stu6=new Student("张6","女");
        Student stu7=new Student("张7","男");
        Student stu8=new Student("张8","男");
        Student stu9=new Student("张9","男");
        Student stu10=new Student("张10","男");

        Collections.addAll(list1,stu1,stu2,stu3,stu4,stu5,stu6,stu7,stu8,stu9,stu10);
        Random r=new Random();
        int time=1;
        while(true) {
            if (time == 1) {
                int i = 0, count = list1.size();
                while (!list1.isEmpty()) {
                    int index = r.nextInt(list1.size());
                    list2.add(list1.get(index));
                    System.out.println(list1.remove(index));
                }
                time++;
                System.out.println("全员已经点名");
            }else if(time==2){
                int i = 0, count = list2.size();
                while (!list2.isEmpty()) {
                    int index = r.nextInt(list2.size());
                    System.out.println(list2.remove(index));
                }
                time++;
                System.out.println("全员已经点名");
            }else if(time==3){
                System.out.println("两次点名完成");
                break;
            }
        }
    }
}
 

(四)自动点名器项目

①准备数据 (爬虫爬取数据+数据处理+生成文件)

方法一:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
    Random r=new Random();
    public static void main(String[] args) throws IOException {
        //定义变量记录网址
        String famliynamenet="https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String girlnamenet="http://www.haoming8.cn/baobao/7641.html";
        String boynamenet="http://www.haoming8.cn/baobao/10881.html";
        //爬取所有网址的字符串内容存入字符串中
        String content1=getString(famliynamenet);
        String content2=getString(girlnamenet);
        String content3=getString(boynamenet);
        //网站内容很多,使用正则表达式筛选自己想要的数据
        ArrayList<String> tempFamliynamelist=selectString(content1,"(\\W{4})(,|。)",1);
        ArrayList<String> tempGirlnamelist=selectString(content2,"(\\W{2}\\s\\W{2}\\s\\W{2}\\s\\W{2}\\s)+",0);
        ArrayList<String> tempBoynamelist=selectString(content3,"([\
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值