私はJavaクラス用の簡単なクラップスシミュレータを作成していますが、何らかの理由で正しく動作していません。 「ポイント」で損失を追跡し、「ポイント」で勝つと考えられていますが、何らかの理由でその値が毎回1または0になる傾向があります。最初のロールでの損失と勝利は機能しているようです。新鮮な目を持つ人が私がどこを乱しているのかを知ることができるかどうか分かります。ありがとうございました!Javaルーピングエラー - 新鮮な目が必要
import java.util.Scanner;
import java.util.Random;
class CrapsSimulator {
public static void main(String[] args) {
// Set up values we will use
int lossfirstroll = 0;
int winfirstroll = 0;
int losswithpoint = 0;
int winwithpoint = 0;
boolean gameover = false;
int point = 0;
// Loop through a craps game 100 times
for (int i = 0; i < 100; i++) {
// First roll -- random number within 2-12
Random rand = new Random();
int random = rand.nextInt(11) + 2;
// Win on first roll
if (random == 7 || random == 11) {
winfirstroll++;
gameover = true;
} // Loss on first roll
else if (random == 2 || random == 3 || random == 12) {
lossfirstroll++;
gameover = true;
} else // Player has "point"
{
point = random;
}
// Check to make sure the game hasn't ended already
while (gameover == false) {
// Reroll the dice
random = rand.nextInt(11) + 2;
// Check to see if player has won
if (random == point) {
winwithpoint++;
gameover = true;
}
// Or if the player has lost
if (random == 7) {
losswithpoint++;
gameover = true;
}
// Otherwise, keep playing
gameover = false;
}
}
// Output the final statistics
System.out.println("Final Statistics\n");
System.out.println("Games played: 100\n");
System.out.println("Wins on first roll: " + winfirstroll + "\n");
System.out.println("Losses on first roll: " + lossfirstroll + "\n");
System.out.println("Wins with point: " + winwithpoint + "\n");
System.out.println("Losses with point: " + losswithpoint + "\n");
}
}
私は乱数のエントロピーに腹を立てている場合に備えて、 'new Random()'をforループの外側に移動したいと思います。 – darvids0n
@ darvids0n良い提案だが役に立たなかった。私はそれを感謝します。 –