一、自动点名器
(一)随机点名
要求:班里若干名学生,对学生进行随机点名
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,"([\

集合综合练习(自动点名器项目)&spm=1001.2101.3001.5002&articleId=143982941&d=1&t=3&u=5952582905ff4964b8256ab85769a43c)
1万+

被折叠的 条评论
为什么被折叠?



