1 JAVA 随机点名器应用案例
package pb.radom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* 随机点名器
* @author TerryZhong
*
*/
public class GetNoEnginery {
public static void main(String[] args) {
List<Integer> noList=new ArrayList<Integer>();//存储回答过问题学员学员的集合
String answer="y";//老师是否继续抽取的答案
Scanner input=new Scanner(System.in);
GetNoEnginery dice=new GetNoEnginery();
System.out.println("请输入班级的最大的学号");
int maxNo=Integer.parseInt(input.nextLine());
do{
if(noList.size()==maxNo){//判断回答过问题学员的个数是否等于最大的学号
System.out.println("所有同学都回答过问题了!");
break;
}
else{
int no=dice.getRandomNo(maxNo)+1;//随机抽取学号
if(!noList.contains(no)){//判断抽取的学号是否已回答过问题???????????????????????????????????????????????????
noList.add(no);//将未回答过的学号存储到集合中
System.out.println("请"+no+"号同学回答问题。。。");
System.out.print("继续抽取学号吗? y/n:");
answer=input.nextLine();
}
}
}while(answer.equalsIgnoreCase("y"));
System.out.println("提问结束!");
}
/**
* 根据种子值随机生成随机数
* @param seed 种子值
* @return
*/
public int getRandomNo(int seed){
Random random=new Random();
return Math.abs(random.nextInt(seed));//返回随机数的绝对值
}
}