0

Java クラス用の単純なクラップス シミュレータを作成していますが、何らかの理由で正常に実行できません。負けは「点」、勝ちは「点」のはずなのですが、なぜかいつも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");
  }
}
4

2 に答える 2

2

デバッガーで実行するかSystem.out.println、ロジックがどこで失敗しているかを確認してください。これは宿題ですか?

于 2011-07-18T07:09:57.490 に答える
1

あなたの問題はgameover旗です。false内側のループの最後で常に再度設定しているため、永久に実行されます。

于 2011-07-18T07:32:23.913 に答える