所以我想学习Java,我写了这段代码,它假设从列表中生成单词的组合,然后放在一个句子中。问题是从仅包含名称的第一个列表中随机选择的单词(名称)将被重用(我知道为什么,但我不确定我是否需要“phrase3”或第三个列表。
这是我的代码:
package ListOfWords;
public class test {
public static void main (String[] args) {
String[] name = {"Nicole","Ronnie","Robbie","Alex","Deb"};
String[] action = {"liar","driver","cook","speller","sleeper","cleaner","soccer
player"};
// find out how many words there are in each list
int nameLength = name.length;
int actionLength = action.length;
// Generate two random numbers
int rand1 = (int) (Math.random() * nameLength);
int rand2 = (int) (Math.random() * actionLength);
String phrase1 = name[rand1];
String phrase2 = action[rand2];
System.out.print("It is obvious that" + ' ' + phrase1 + " " + "is a better" + " " +
phrase2 + " " + "than" + " " + phrase1 + "!" );
}
}
这是我目前得到的结果:
It is obvious that Robbie is a better cleaner than Robbie!
因此,当您看到第一个列表中的随机名称被重用时 - 我如何确保它不会从第一个列表中选择相同的元素(名称)?