不使用ArrayList:
import java.util.*;
class Dianming
{
public static void main(String[] args)
{
String[] str = {"聂晓冬","谢晓明","杜弦东","周明凡","张永超","林冲","陈新"
,"王庆","卢威","李训文","谢大根","贺维华","邵嘉威","符帮春","文巍","吴应桐"
,"周长青","王志强","赖佛来","张广","孙伟","黄振源","陆滔","陈德强","张泽鹏"
,"谢剑东","沈炎","龙泽锋","羊定志","刘超","孟伟","罗铖","魏文彬","黄鹏"
,"高超","曾强","潘悦","廖畅勇","徐文豪","张晓坤","刘高","唐海"
,"许佳园","彭波","曹健","戴莉华","罗英标","肖凯元","魏厚国","谭敦勇","胡银"
,"陆先俊","周志明","冷晓龙","张杰","陈虹睿","王庚","王云","张宏文"
,"邹青青","李彦东","覃千伦","张智聪","张琪","陆安","吴斌洋","黄章挺","李志"
,"陈思齐","李建军","黄正根","张健","林思齐","杨俊","徐亮","杨文君","陈圣安","史鹏科"};
Scanner sc = new Scanner(System.in);
System.out.println("请输入1点名,或者输入0结束");
int i = 0;
while(true)
{
int q=sc.nextInt();
if(q==1)
{
int random = (int)(Math.random()*(str.length-i));
i++;
for (int x = 0 ; x < str.length; x++)
{
if(str[x].equals(""))
{
random++;
}
else if(x == random)
{
System.out.println("第"+i+"次点名请"+str[random]+"回答问题");
str[random]
="";
break;
}
}
}
else if(q==0)
{
System.out.println("点名结束");
break;
}
else
{
System.out.println("请输入1继续点名,或者输入0结束");
}
if(i==str.length){
System.out.println("点名结束");
break;
}
}
}
}
使用ArrayList:
import java.util.*;
class Dianmingqi
{
public static void main(String[] args)
{
String[] str = {"聂晓冬","谢晓明","杜弦东","周明凡","张永超","林冲","陈新"
,"王庆","卢威","李训文","谢大根","贺维华","邵嘉威","符帮春","文巍","吴应桐"
,"周长青","王志强","赖佛来","张广","孙伟","黄振源","陆滔","陈德强","张泽鹏"
,"谢剑东","沈炎","龙泽锋","羊定志","刘超","孟伟","罗铖","魏文彬","黄鹏"
,"高超","曾强","潘悦","廖畅勇","徐文豪","张晓坤","刘高","唐海"
,"许佳园","彭波","曹健","戴莉华","罗英标","肖凯元","魏厚国","谭敦勇","胡银"
,"陆先俊","周志明","冷晓龙","张杰","陈虹睿","王庚","王云","张宏文"
,"邹青青","李彦东","覃千伦","张智聪","张琪","陆安","吴斌洋","黄章挺","李志"
,"陈思齐","李建军","黄正根","张健","林思齐","杨俊","徐亮","杨文君","陈圣安","史鹏科"};
ArrayList<String> al = new ArrayList<String>();
for (int x = 0 ;x < str.length ;x++ )
{
al.add(str[x]);
}
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入1点名,或者输入0结束");
while(true)
{
int next = sc.nextInt();
if(next == 1)
{
if(count == str.length)
{
System.out.println("点名结束");
break;
}
int random = (int)(Math.random()*(str.length-count));
count++;
System.out.println("第"+count+"次点名,请"+al.remove(random)+"回答问题");
}
else if(next == 0)
{
System.out.println("点名结束");
break;
}
else
{
System.out.println("请输入1继续点名,或者输入0结束");
}
}
}
} |
|