2017-03-19 6 views
-3
 List<String> n = new ArrayList<String>(); 
n.add("Allen"); 
n.add("beer"); 
n.add("Johnson"); 
n.add("bar"); 
n.add("girl"); 
    List<String> v = new ArrayList<String>(); 
v.add("shot"); 
v.add("drank"); 
v.add("ate"); 
v.add("hit"); 
v.add("tossed"); 
    int noun = (int)(Math.random() * n.size()); 
    int verb = (int)(Math.random() * v.size()); 
    int noun2 = (int)(Math.random() * n.size()); 
    int verb2 = (int)(Math.random() * v.size()); 
    int noun3 = (int)(Math.random() * n.size()); 
    String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3)); 

    int x = 0; 
    while (x < 5) { 
     System.out.println(a); 
    x++; 
} 

その後のような一つだけの文繰り返して進ん:ジョンソンはジョンソンのジョンソンはバーを投げ、バーなぜ私のwhileループは同じ文をランダムに繰り返すのですか?

を打つ棒を投げ、バー

を打つ棒を投げ、バー

を打つ

をジョンソン投げバーとヒットバー

ジョンソン投げ棒とヒットバー

どのようにランダムにして毎回違うのですか?

+0

何が正確にランダム化したいですか? – Adonis

+0

ループ内でランダム化が発生した場合、値は決して変更されません – efekctive

答えて

1

文字列の割り当てを1回だけ実行しています。だから常に同じになるでしょう。

int x = 0; 
while (x < 5) { 
    int noun = (int)(Math.random() * n.size()); 
    int verb = (int)(Math.random() * v.size()); 
    int noun2 = (int)(Math.random() * n.size()); 
    int verb2 = (int)(Math.random() * v.size()); 
    int noun3 = (int)(Math.random() * n.size()); 
    String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3)); 
    System.out.println(a); 
    x++; 
} 
+0

ループ内で毎回乱数を生成していないため、ソリューションには現在の欠陥OPがあります。 –

+0

@OusmaneMahyDiawは、その部分を完全にスキップしました。更新されます、ありがとう –

1

あなたは1つの文字列を使用する代わりに、ランダムな文字列を再生成する必要があります:あなたはそうと同じように、何度も再生成する場合は、ループ内で

String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3)); 

:あなたの割り当てを入れてください。ランダムロジックをwhileループに置くだけです。試してみることができます:

List<String> n = new ArrayList<String>(); 
    n.add("Allen"); 
    n.add("beer"); 
    n.add("Johnson"); 
    n.add("bar"); 
    n.add("girl"); 
    List<String> v = new ArrayList<String>(); 
    v.add("shot"); 
    v.add("drank"); 
    v.add("ate"); 
    v.add("hit"); 
    v.add("tossed"); 

    int x = 0; 
    while (x < 5) { 
     int noun = (int)(Math.random() * n.size()); 
     int verb = (int)(Math.random() * v.size()); 
     int noun2 = (int)(Math.random() * n.size()); 
     int verb2 = (int)(Math.random() * v.size()); 
     int noun3 = (int)(Math.random() * n.size()); 
     String a = n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3); 

     System.out.println(a); 
     x++; 
    } 
1

whileループを実行するたびにランダム変数を「再ランダム化」する必要があります。変数を宣言するのにMath.random()を使用しても、変数を使用するたびに変更されることはありません。変数を宣言すると、変数に新しい値を与えて変数を変更しない限り、変数は変更されません。だから、あなたはこれをしたいと思う:

int x = 0; 
while (x < 5) { 
    int noun = (int)(Math.random() * n.size()); 
    int verb = (int)(Math.random() * v.size()); 
    int noun2 = (int)(Math.random() * n.size()); 
    int verb2 = (int)(Math.random() * v.size()); 
    int noun3 = (int)(Math.random() * n.size()); 
    String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3)); 

    System.out.println(a); 
    x++; 
} 

この道を、あなたは、whileループの反復ごとに、その上、nounの値を変更verb、とされています。

1

whileループを入力する前に、あなたの "ランダムな"変数を設定しています。したがって、whileループの繰り返しごとに、同じ値を使用しています。各反復で異なる結果を得るためには、whileループ内に乱数生成を含める必要があります。

int x = 0; 
while (x < 5) { 
    int noun = (int)(Math.random() * n.size()); 
    int verb = (int)(Math.random() * v.size()); 
    int noun2 = (int)(Math.random() * n.size()); 
    int verb2 = (int)(Math.random() * v.size()); 
    int noun3 = (int)(Math.random() * n.size()); 
    String a = new String(n.get(noun) + " " + v.get(verb) + " " + n.get(noun2) + " " + "and" + " " + v.get(verb2) + " " + n.get(noun3)); 

    System.out.println(a); 
    x++; 

}

関連する問題