Java 识别布尔逻辑路径错误

Java 识别布尔逻辑路径错误,java,boolean,logic,dice,Java,Boolean,Logic,Dice,在游戏中的某个地方有一个逻辑错误,与我的赢和输布尔值有关,使游戏在第一次掷骰时打印出赢或输,以及最终值始终为真,但我不确定它在哪里 我最好的猜测是它在哪里检查输赢情况 package bergman.java.ass2; import java.util.Random; /** * * @author Jason */ public class GameSimulation { public static void main(String[] args) { //create dic

在游戏中的某个地方有一个逻辑错误,与我的赢和输布尔值有关,使游戏在第一次掷骰时打印出赢或输,以及最终值始终为真,但我不确定它在哪里

我最好的猜测是它在哪里检查输赢情况

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;
        }
      }
    }

}
包bergman.java.ass2;
导入java.util.Random;
/**
*
*@作者杰森
*/
公共类游戏模拟{
公共静态void main(字符串[]args){
//创建骰子
int die1,die2;
//为wins和程序运行时间创建计数器
int runsCenter,winsCenter=0,loseCounter=0;
//创建变量以检查它是否为初始滚动
布尔第一卷;
//创建一个变量来存储滚动的初始点
int rollPoint=0;
//创建变量以存储随机数
Random rand=新的Random();
//创建一个变量以检查赢/输是否为真
布尔赢=假,输=假;
对于(;运行中心<10000;){
//掷骰子进行第一次掷骰
第一卷=真;
die1=兰特·耐克斯汀(6)+1;
die2=兰德·耐克斯汀(6)+1;
//检查是否是第一卷
if(firstRoll=true){
//如果第一卷是7或11,则添加一个胜利
如果(die1+die2==7 | | die1+die2==11){
System.out.print(“你赢了!有转鼓”+die1+“和”
+die2+“\n”);
第一卷=假;
赢=真;
}
//如果第一卷是亏损,则增加亏损
如果(die1+die2==2 | die1+die2==3 | die1+die2==12){
System.out.print(“你输了!带转鼓”+die1+“和”
+die2+“\n”);
第一卷=假;
失去=真实;
} 
//如果两者都不是,则存储卷并结束循环
否则{
rollPoint=die1+die2;
第一卷=假;
}
}
//检查第一次掷骰是赢还是输,如果不是继续掷骰
for(;win==false&&lose==false;){
die1=兰特·耐克斯汀(6)+1;
die2=兰德·耐克斯汀(6)+1;
//检查新卷是否与第7点或第7点匹配
如果(die1+die2==滚动点){
赢=真;
}否则如果(die1+die2==7){
失去=真实;
}
}
//在循环中利用赢/输陈述
如果(win=true){
winsCounter=winsCounter++;
System.out.print(“你赢得了转鼓”+die1
+“和“+die2+”,带“+rollPoint”的点滚动);
赢=假;
}else if(lose=true){
System.out.print(“积分前滚动7分会导致失败”);
loseCounter=loseCounter++;
丢失=错误;
}
}
}
}

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

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

if (firstRoll == true) {...
if (win == true) {...
} else if (lose == true) {...
我相信这是你的逻辑问题,因为一旦你到达终点,即使输赢是真的,赢是假的,你将赢设为真,并且只进入赢声明的主体

另一个原因是,您从未增加RunsCenter,因此

for(;runsCounter < 10000;)
为了防止程序永远循环,您需要运行10000次

for(;runsCounter < 10000; runsCounter++)

以及

loseCounter = loseCounter++;

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

这是修改后的代码

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);
}
publicstaticvoidmain(字符串[]args){
//创建骰子
int die1,die2;
//为wins和程序运行时间创建计数器
int WinsCenter=0,loseCounter=0;
//创建变量以检查它是否为初始滚动
布尔第一卷;
//创建一个变量来存储滚动的初始点
int rollPoint=0;
//创建变量以存储随机数
Random rand=新的Random();
//创建一个变量以检查赢/输是否为真
布尔赢=假,输=假;
对于(int-runsconter=0;runsconter<10000;runsconter++){
//掷骰子进行第一次掷骰
第一卷=真;
die1=兰特·耐克斯汀(6)+1;
die2=兰德·耐克斯汀(6)+1;
//检查是否是第一卷
if(firstRoll==true){
//如果第一卷是7或11,则添加一个胜利
如果(die1+die2==7 | | die1+die2==11){
System.out.print(“你赢了!有转鼓”+die1+“和”
+die2+“\n”);
第一卷=假;
赢=真;
}
//如果第一卷是亏损,则增加亏损
如果(die1+die2==2 | die1+die2==3 | die1+die2==12){
System.out.print(“你输了!带转鼓”+die1+“和”
+die2+“\n”);
第一卷=假;
失去=真实;
} 
//如果两者都不是,则存储卷并结束循环
否则{
rollPoint=die1+die2;
第一卷=假;
}
}
//检查第一次掷骰是赢还是输,如果不是继续掷骰
for(;win==false&&lose==false;)
{
die1=兰特·耐克斯汀(6)+1;
die2=兰德·耐克斯汀(6)+1;
//检查新卷是否与第7点或第7点匹配
如果(die1+die2==滚动点){
赢=真;
}否则如果(die1+die2==7){
失去=真实;
}
}
//在循环中利用赢/输陈述
如果(win==真){
winsCounter=winsCounter++;
System.out.print(“你赢得了转鼓”+die1
+“和“+die2+”以及“+rollPoint+”\n”)的点滚动;
赢=假;
}else if(lose==true){
System.out.print(“在得分前滚动7分会导致失败\n”
winsCounter++;
loseCounter = loseCounter++;
loseCounter++;
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);
}