写一个程序打印从命令行传入的字符串(几个字符串),然后随机输出字符串(用hashset来完成)

java

public class Test1 {
public static void main(String[] args) {
Set<String> out=new HashSet<String>();
String index = "";
Scanner sc=new Scanner(System.in);
String str = "";
while(!str.equals("y")){
str=sc.next();
out.add(str);
}
Iterator<String> it=out.iterator();
while(it.hasNext()){
double i=Math.random();
if(i<0.5){
index=it.next();
if(!index.equals("y"))
System.out.println(index);
}
else
{}
}
}
}

当输入y时就会输出了!
温馨提示:答案为网友推荐,仅供参考