【问题标题】:Identifying a boolean logical pathing error识别布尔逻辑路径错误
【发布时间】:2018-01-23 05:39:49
【问题描述】:

游戏中某处存在逻辑错误,这与我的输赢布尔值有关,导致游戏在第一轮打印出输赢,并且最终值始终为真,但我'不知道它在哪里。

我最好的猜测是它检查输赢条件的位置。

package bergman.java.ass2;

import java.util.Random;

/**
 *
 * @author Jason
 */
public class GameSimulation {

public static void main(String[] args) {
//create dice
    int die1, die2;
//create the counters for the wins and the times the program runs
    int runsCounter, winsCounter = 0, loseCounter = 0;
//Create the variable to check whether it is the initial roll or not
    boolean firstRoll;
//create a variable to store the initial point of the roll
    int rollPoint = 0;
//Create the variable to store the random number 
    Random rand = new Random();
//Create a variable to check whether a win/loss is true or not
    boolean win = false, lose = false;
for(;runsCounter < 10000;){
//roll the dice for initial roll
        firstRoll = true;
        die1 = rand.nextInt(6) + 1;
        die2 = rand.nextInt(6) + 1;

//check if it's the first roll
        if (firstRoll = true) {
    //if it's a 7 or 11 on first roll add a win
            if (die1 + die2 == 7 || die1 + die2 == 11) {
                System.out.print("you win! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                win = true;

            }
    //if it's a loss on the first roll add a loss
            if (die1 + die2 == 2 || die1 + die2 == 3 || die1 + die2 == 12) {
                System.out.print("You lose! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                lose = true;

            } 
    //if it's neither, store the roll and end the loop
            else {
                rollPoint = die1 + die2;
                firstRoll = false;
            }
        }
    //check if first roll was a win or loss, if not continue rolling
        for (; win == false && lose == false;) {
            die1 = rand.nextInt(6) + 1;
            die2 = rand.nextInt(6) + 1;
    //check if the new roll matches point or 7
            if (die1 + die2 == rollPoint) {
                win = true;
            } else if (die1 + die2 == 7) {
                lose = true;
            }
        }

    //utilize win/loss statements within loop
        if (win = true) {
            winsCounter = winsCounter++;
            System.out.print("You win with rolls " + die1
                    + " and " + die2 + " with a point roll for " + rollPoint);
            win = false;
        } else if (lose = true) {
            System.out.print(" you lose by rolling a 7 before point score");
            loseCounter = loseCounter++;
            lose = false;
        }
      }
    }

}

【问题讨论】:

    标签: java boolean logic dice


    【解决方案1】:

    此代码存在一些问题,其中一个是您在 if 语句中进行赋值,当您应该进行比较时,该语句的计算结果始终为 true。这些部分:

    if (firstRoll = true) {...
    if (win = true) {...
    } else if (lose = true) {...
    

    应该是

    if (firstRoll == true) {...
    if (win == true) {...
    } else if (lose == true) {...
    

    我相信这是你的逻辑问题,因为一旦你到达终点,即使 loss 是 true 并且 win 是 false,你将 win 设置为 true 并且只进入 win 语句的主体。

    另一个是你永远不会增加 runsCounter 所以这条线

    for(;runsCounter < 10000;)
    

    等价于

    while(true)
    

    为了防止程序永远循环并执行您需要的 10,000 次运行

    for(;runsCounter < 10000; runsCounter++)
    

    然后要跟踪您需要更改的赢/输

    winsCounter = winsCounter++;
    

    winsCounter++;
    

    还有

    loseCounter = loseCounter++;
    

    loseCounter++;
    

    这里的问题是,例如,winsCounter 最初为 0,当您执行 winsCounter++ 时,您会创建一个具有当前值 (0) 的副本,然后将 winsCounter 递增到 1,然后返回副本 (0) 并设置 winsCounter为 0。 more information on this

    这是修改后的代码

    public static void main(String[] args) {
    //create dice
    int die1, die2;
    //create the counters for the wins and the times the program runs
    int winsCounter = 0, loseCounter = 0;
    //Create the variable to check whether it is the initial roll or not
    boolean firstRoll;
    //create a variable to store the initial point of the roll
    int rollPoint = 0;
    //Create the variable to store the random number 
    Random rand = new Random();
    //Create a variable to check whether a win/loss is true or not
    boolean win = false, lose = false;
    for(int runsCounter = 0;runsCounter < 10000; runsCounter++){
    //roll the dice for initial roll
        firstRoll = true;
        die1 = rand.nextInt(6) + 1;
        die2 = rand.nextInt(6) + 1;
    
    //check if it's the first roll
        if (firstRoll == true) {
        //if it's a 7 or 11 on first roll add a win
            if (die1 + die2 == 7 || die1 + die2 == 11) {
                System.out.print("you win! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                win = true;
    
            }
    //if it's a loss on the first roll add a loss
            if (die1 + die2 == 2 || die1 + die2 == 3 || die1 + die2 == 12) {
                System.out.print("You lose! with rolls " + die1 + " and "
                        + die2 + "\n");
                firstRoll = false;
                lose = true;
    
            } 
    //if it's neither, store the roll and end the loop
            else {
                rollPoint = die1 + die2;
                firstRoll = false;
            }
        }
    //check if first roll was a win or loss, if not continue rolling
        for (; win == false && lose == false;) 
        {
            die1 = rand.nextInt(6) + 1;
            die2 = rand.nextInt(6) + 1;
    //check if the new roll matches point or 7
            if (die1 + die2 == rollPoint) {
                win = true;
            } else if (die1 + die2 == 7) {
                lose = true;
            }
        }
    
    //utilize win/loss statements within loop
        if (win == true) {
            winsCounter = winsCounter++;
            System.out.print("You win with rolls " + die1
                    + " and " + die2 + " with a point roll for " + rollPoint+"\n");
            win = false;
        } else if (lose == true) {
            System.out.print(" you lose by rolling a 7 before point score\n");
            loseCounter++;
            lose = false;
        }
      }
    
      System.out.println("Wins: " + winsCounter + " Losses: " + loseCounter);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多