需要创建一个从数组列表中选择随机数组的循环

时间:2015-09-07 20:50:45

标签: java arrays random

我需要从shoutOutRandomMessage()中的每个数组中选择一个随机单词,然后按此顺序打印所选单词:( subject-verbs-adjectives-objects-adverb)。

我尝试了循环并使用了随机生成器,但它打印了数组列表中的所有对象,而不是每个数组列表中的一个随机选择的对象。

package milestone.pkg4;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Milestone4 {
 public static void main(String[] args){
    new Milestone4().shoutOutCannedMessage();

}
 public String shoutOutCannedMessage(){
    String[] messages;
     messages= new String[10];
      messages[0]="I like HipHop";
      messages[1]="I like Rock and Roll";
      messages[2]="I like movies";
      messages[3]="I don't like action films";      
      messages[4]="I'll watch anything with Jackie chan!";      
      messages[5]="Will Smith is a great actor";        
      messages[6]="Lebron James is a beast";        
      messages[7]="I like homecooked food";
      messages[8]="I like chinese food";        
      messages[9]="I love pizza";

    for (int i=0; i<messages.length; i++)
    {
        System.out.println("choice["+i+"]:" + messages[i]);//prints messages
    }
      System.out.print("Please choose the message to which you relate: ");
    //user input section
      Scanner userInput= new Scanner(System.in);
        int sel= userInput.nextInt();/**looks for the number a user selects
        * as their response*/
        String userSelection= messages[sel];

        System.out.println(userSelection);
        return null;
 }

//Random shout
   public String shoutOutRandomMessage(){
     String[] subjects={"law", "football", "movies"};
     String[] objects={"ball", "knife", "sneaker"};
     String[] verbs={"fleeing", "thrashing", "petting"};
     String[] adverb={"softly", "belligerently", "expressively"};
     String[] adjectives={"disgusting", "short", "bubbly"};

//random number generator**

3 个答案:

答案 0 :(得分:0)

以下代码:

for (int i=0; i<messages.length; i++)
{
    System.out.println("choice["+i+"]:" + messages[i]);//prints messages
}

打印所有消息。你可能想要评论一下。

此外,您可能希望通过完整代码(获取随机数)。

答案 1 :(得分:0)

我无法看到shoutOutRandomMessage()的调用位置,但要使该方法返回一个由其中声明的数组中的随机字组成的字符串,请尝试使用

//Random shout


public String shoutOutRandomMessage(){
     String[] subjects={"law", "football", "movies"};
     String[] objects={"ball", "knife", "sneaker"};
     String[] verbs={"fleeing", "thrashing", "petting"};
     String[] adverb={"softly", "belligerently", "expressively"};
     String[] adjectives={"disgusting", "short", "bubbly"};

StringBuilder builder= new StringBuilder();
Random random = new Random();
builder.append(subjects[random.nextInt(3)]);
builder.append(objects[random.nextInt(3)]);
builder.append(verbs[random.nextInt(3)]);
builder.append(adverb[random.nextInt(3)]);
builder.append(adjectives[random.nextInt(3)]);

return builder.toString();
//random number generator**

答案 2 :(得分:0)

您可以尝试这样的事情:

// create a random number generator, you can not ensure pure randomness but it is near to it
try {
      private SecureRandom numberGenerator  numberGenerator = SecureRandom.getInstance("SHA1PRNG");
    } catch(NoSuchAlgorithmException nsae) {
       nsae.printStackTrace();
    }

//subjects-verbs-adjectives-objects-adverb = 5 outputs
for(int i = 0 ; i<4; i++){
    // switch over the our loop and print the specific random word
    switch(i){
         case 0: System.out.println(subjects[numberGenerator.nextInt(subjects.size())]);break;
         case 1: System.out.println(verbs[numberGenerator.nextInt(verbs.size())]);break;
         case 2: System.out.println(adjectives[numberGenerator.nextInt(adjectives.size())]);break;
         case 3: System.out.println(objects[numberGenerator.nextInt(objects.size())]);break;
         case 4: System.out.println(adverb[numberGenerator.nextInt(adverb.size())]);break;
         default:break;
    }
}

有关SecureRandom类的文档,请查看:https://docs.oracle.com/javase/6/docs/api/java/security/SecureRandom.html