游戏中的某个地方存在一个逻辑错误,与我的胜利有关,并且输掉了布尔值,使得游戏在第一次滚动时打印出输赢,以及最终值始终为真,但是我我不知道它在哪里。
我最好的猜测是检查输赢情况。
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;
}
}
}
}
答案 0 :(得分:-1)
这段代码存在一些问题,一个是你在if语句中做的赋值,当你应该进行比较时,它总是会被评估为true。这些部分:
if (firstRoll = true) {...
if (win = true) {...
} else if (lose = true) {...
应该是
if (firstRoll == true) {...
if (win == true) {...
} else if (lose == true) {...
我相信这是你的逻辑问题,因为一旦你到达终点,即使输的是真的,胜利是假的,你将胜利设置为真,只进入胜利声明的主体内。
另一个是你永远不会增加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);
}