자바 클래스 용 간단한 크랩 시뮬레이터를 쓰고 있는데, 어떤 이유로 든 제대로 실행하는 데 문제가 있습니다. "포인트"로 손실을 추적하고 "포인트"로 이기기로되어 있지만, 어떤 이유로 그 값은 매번 1 또는 0이되는 경향이 있습니다. 첫 번째 롤에서의 손실과 승리가 효과가있는 것 같습니다. 새로운 눈을 가진 사람이 내가 엉망인 곳을 알아낼 수 있는지 궁금합니다. 고맙습니다!자바 루핑 오류 - 신선한 눈이 필요합니다.
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");
}
}
랜덤 넘버의 엔트로피가있는 경우에 대비해서 for 루프 외부로'new Random() '을 옮깁니다. – darvids0n
@ darvids0n 좋은 제안이지만 도움이되지 못했습니다. 나는 그것을 고맙게 생각한다. –