3

所以我想学习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!

因此,当您看到第一个列表中的随机名称被重用时 - 我如何确保它不会从第一个列表中选择相同的元素(名称)?

4

4 回答 4

3

您需要第三个随机数和短语,以选择要使用的第二个名称。例如:

// Generate two random numbers 
   int  rand1 = (int) (Math.random() * nameLength);
   int  rand2 = (int) (Math.random() * actionLength);
   int  rand3 = (int) (Math.random() * nameLength);

   String phrase1 = name[rand1];
   String phrase2 = action[rand2];
   String phrase3 = name[rand3];

   System.out.print("It is obvious that" + ' ' + phrase1 + " " + "is a better" + " " +  
   phrase2 + " " + "than" + " " + phrase3 + "!" );

编辑:为避免为短语 1 和短语 3 选择相同名称的可能性,以下代码应确保使用与短语 1 不同的索引来选择短语 3:

int  rand1 = (int) (Math.random() * nameLength);
int  rand2 = (int) (Math.random() * actionLength);
int  rand3 = (int) (Math.random() * nameLength);
while(rand1==rand3){
    rand3 = (int) (Math.random() * nameLength);
}

这将导致 rand3 被更改,直到它与 rand1 不同,这将为phrase1 和phrase3 选择不同的名称。

请注意,如果名称数组中只有一个名称,这将导致无限循环。

于 2013-07-27T23:21:41.303 回答
2

你可以这样做:

List<String> randomNames = new ArrayList(Arrays.asList(name));
Collections.shuffle(randomNames);

int randAction = (int) (Math.random() * actionLength);

String phrase1 = randomNames.get(0);
String phrase2 = action[randAction];
String phrase3 = randomNames.get(1);

System.out.print("It is obvious that " +  phrase1 + " is a better " 
     +  phrase2 + " than " + phrase3 + "!" );   
于 2013-07-27T23:30:03.153 回答
1
   //Generate two random numbers 
   int  rand1 = (int) (Math.random() * nameLength);
   int  rand2 = (int) (Math.random() * actionLength);
   int  rand3;
   do{
       rand3 = (int) (Math.random() * nameLength)
   } while (rand3 == rand1);

   String phrase1 = name[rand1];
   String phrase2 = action[rand2];
   String phrase3 = name[rand3];

   System.out.print("It is obvious that" + ' ' + phrase1 + " " + "is a better" + " " +  
   phrase2 + " " + "than" + " " + phrase3 + "!" );
于 2013-07-27T23:34:53.123 回答
0

例如,您似乎正在将 random 初始化为 4 。然后每次调用该索引时,您都会得到相同的值。

在该结构中,您将需要一个新变量。

如果您查看程序的流程,您将创建两个 radoms。然后设置它们。他们绝不会重新初始化。

添加另一个变量来求解或创建一个函数来返回一个新的 rand 并将其传递给我

看到您对第 3 阶段可能相同的评论。

从下面的评论。首先为名称列表创建一个带有索引的数组。随机选择一个值。将此索引值替换为列表中的最后一个值,并选择另一个长度为 1 的值。– Jongware 27 分钟前。魔法。

于 2013-07-27T23:22:31.307 回答