2017-02-05 4 views
-1

私はプログラミングの世界には新しいので、もしあなたの誰かが私がこれでより良くなるのを助けることができれば、深く感謝します。このプログラムの私の目標は、3000回のダイスロールをシミュレートし、whileループを使用して、ダブルスの可能なペアごとにダブルスが回される回数を数えることです。結果はダイアログボックスに出力する必要があります。3000回のダイスロールをシミュレートし、ダブルスの回数をカウントするにはどうすればよいですか? JAVA

Random random; 
    random = new Random(); 
    diceRolls = 1; 
    snakeEyes = 1; 
    doubleTwos = 1; 
    doubleThrees = 1; 
    doubleFours = 1; 
    doubleFives = 1; 
    doubleSixes = 1; 


    while (diceRolls <= finalDiceRoll) { 
     int diceRolls = 1; 
     int die1 = random.nextInt(6) + 1; 
     int die2 = random.nextInt(6) + 1; 


     if (die1 == 1 && die2 == 1){ 
      //snakeEyes = snakeEyes + 1;  
      snakeEyes++; 

     } 
     else if (die1 == 2 && die2 == 2) { 
      doubleTwos++; 
     } 
     else if (die1 == 3 && die2 == 3) { 
      doubleThrees++; 
     } 
     else if (die1 == 4 && die2 == 4) { 
      doubleFours++; 
     } 
     else if (die1 == 5 && die2 == 5) { 
      doubleFives++; 
     } 
     else if (die1 == 6 && die2 == 6) { 
      doubleSixes++; 
     } 
     JOptionPane.showMessageDialog (null, "You rolled snake eyes " + snakeEyes + " times\nYou rolled double twos " + doubleTwos + " times\nYou" 
            + " rolled double threes " + doubleThrees + " times\nYou rolled double fours " + doubleFours + " times\nYou" 
            + " rolled double fives " + doubleFives + " times\nYou rolled double sixes " + doubleSixes + " times"); 

} 

私がここにいる問題は、私がプログラムから得ている結果が「もっともらしい」と思われないことです。例えば、3000のダイスロールのうち、私は各ダブルの1ペアを取得します。私は間違って何をしていますか?

+0

すでに1対に初期化された各ペアがあります。 プログラムから2倍のペアはありませんか? –

+1

各サイコロの後にダイアログを表示します。最終結果を得るには、それを3000回クリックしなければなりません。 – Bombe

答えて

1

showMessageDialogをwhileループの外側に移動し、diceRolls変数をインクリメントします。他のすべての整数変数を0で初期化してください。
2次元配列またはマップを使用する方が良い(よりクリーンで短く)アプローチです。

Random random = new Random(); 
int snakeEyes = 0; 
int doubleTwos = 0; 
int doubleThrees = 0; 
int doubleFours = 0; 
int doubleFives = 0; 
int doubleSixes = 0; 
int diceRolls = 1; 
while (diceRolls <= 3000) { 
    int die1 = random.nextInt(6) + 1; 
    int die2 = random.nextInt(6) + 1; 

    if (die1 == 1 && die2 == 1) { 
     snakeEyes++; 
    } else if (die1 == 2 && die2 == 2) { 
     doubleTwos++; 
    } else if (die1 == 3 && die2 == 3) { 
     doubleThrees++; 
    } else if (die1 == 4 && die2 == 4) { 
     doubleFours++; 
    } else if (die1 == 5 && die2 == 5) { 
     doubleFives++; 
    } else if (die1 == 6 && die2 == 6) { 
     doubleSixes++; 
    } 
    diceRolls++; 
} 
JOptionPane.showMessageDialog(null, "You rolled snake eyes " + snakeEyes + " times\nYou rolled double twos " + doubleTwos + " times\nYou" 
     + " rolled double threes " + doubleThrees + " times\nYou rolled double fours " + doubleFours + " times\nYou" 
     + " rolled double fives " + doubleFives + " times\nYou rolled double sixes " + doubleSixes + " times"); 

上記の短いバージョン:あなたのコードで確認する必要があり

Random random = new Random(); 
int[] doubled = new int[6]; 

for (int diceRolls = 0; diceRolls < 3000; diceRolls++) { 
    int die1 = random.nextInt(6); 
    int die2 = random.nextInt(6); 

    if (die1 == die2) 
     doubled[die1]++; 
} 
JOptionPane.showMessageDialog(null, "You rolled snake eyes " + doubled[0] + " times\nYou rolled double twos " + doubled[1] + " times\nYou" 
     + " rolled double threes " + doubled[2] + " times\nYou rolled double fours " + doubled[3] + " times\nYou" 
     + " rolled double fives " + doubled[4] + " times\nYou rolled double sixes " + doubled[5] + " times"); 
1

いくつかの変更、

1.0によってすべての変数を初期化します。

while (diceRolls <= finalDiceRoll) // hoping that finalDiceRoll = 3000 

に変化while-loop状態をwhile-loopからint diceRolls = 1;を除去し、while-loopの終わりにdiceRolls++;を加えます。

4. の外側にお客様のJOptionPaneを入れてください。そうでない場合は、JOptionPaneのデイログを3000回閉じなければなりません。

コードにこれらの変更を適用すると、何が間違っていて、コードがうまく動作するかがわかります。

0

次のようにカウントロジックが行く必要があります。

doubleTwos = 0; 
doubleThrees = 0; 
doubleFours = 0; 
doubleFives = 0; 
doubleSixes = 0; 

finalDiceRoll = 3000; 
int diceRolls = 0; 
while (diceRolls < finalDiceRoll) { 
    ++diceRolls; 

任意のメッセージループの後に。

int[] doubleCounts = new int[6]; // By 0 based dice values 
for (int i = 0; i < finalDiceRoll; ++i) { 
    int die1 = random.nextInt(6); // With dice values 0-5 
    int die2 = random.nextInt(6); 

    if (die1 == die2) { 
     ++doubleCounts[die1]; 
    } 
} 
int snakeEyes = doubleCounts[0]; 
int doubleTwos = doubleCounts[1]; 
... 
0

簡素化:あなたが入力して保存することがArrayで

。重複を排除する。配列を使用できるときは、複数の変数を使用しないでください。

int[] doubles = new int[7]; // make the index the number rolled (ignore index 0) 
int diceRolls = 0; 
while (diceRolls++ < 3000) { 
    int die1 = random.nextInt(6) + 1; 
    int die2 = random.nextInt(6) + 1; 
    if (die1 == die2) 
     doubles[die1]++; 
} 
String output = ""; 
for (int i = 1; i <= 6; i++) 
    output += "You rolled double " + i + "'s " + doubles[i] + " times\n"; 

JOptionPane.showMessageDialog (null, output); 

これだけです。

+0

'StringBuilder'のインスタンスがケーキのアイシングになります。 –

+0

@GrzegorzGórkiewicz私はそれを初心者のために簡単に保つようにしていました。短い文字列を連結するのは6回しかないので、 '+'は十分です。 – Bohemian

関連する問題