【发布时间】: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;
}
}
}
}
【问题讨论】: