输入几个词语,怎样用java随机组句

如题所述

第1个回答  2017-07-18
String[] words = new String[5];
Scanner sc = new Scanner(System.in);
Random r = new Random();
for(int i = 0; i < words.length; i++){
String word = sc.nextLine();
while(true){
int index = r.nextInt(5);
if(words[index] == null){
words[index] = word;
break;
}
}
}
for(String word : words){
System.out.print(word);
}

相似回答