总的来说,我对 stackoverflow 和 java 很陌生。我目前正在开发一款名为 Craps 的掷骰子程序游戏,但我很难找到错误。
public class Craps {
public static Random rand; //INITIALIZE RANDOM
public static Scanner in; //INITIALIZE USER INPUT
public static int numOfDice = 2; //INITIALIZE NUMBER OF DICES INVOLVED
public static int numOfSides = 6; //INITIALIZE NUMBER OF SIDES INVOLVED
static {
rand = new Random();
in = new Scanner(System.in);
}
public static int roll() { //ROLL DICE
int dice1 = rand.nextInt(numOfDice + (numOfSides+1));//ROLL DICE 1
int dice2 = rand.nextInt(numOfDice+(numOfSides+1)); //ROLL DICE 2
int roll = dice1 + dice2; //STORE THE VALUE OF THE TWO DICES
return roll;
}
public static boolean round() {
int firstPoint = roll(); //CHECK THE VALUE OF THE FIRST ROLL
if (firstPoint == 7 || firstPoint == 11) { //IF THE VALUE IS EITHER 7 OR 11 IN THE FIRST ROLL YOU WIN
System.out.println("You win!");
} else if (firstPoint == 2 || firstPoint == 3 || firstPoint == 12) { //IF THE VALUE IS EITHER 2 3 OR 12 IN THE FIRST ROLL YOU LOSE
System.out.println("You lose!");
} else { //IF THE VALUE IS NEITHER OF THOSE ROLL AGAIN
int secPhase = firstPoint;
secPhase = 0;
while (firstPoint != secPhase && firstPoint !=7) { //LOOP THIS UNTIL YOU GET AN OUTCOME
if (secPhase == 7) {
System.out.println("You lose!"); //IF THE VALUE IS 7 YOU LOSE
} else if (secPhase == firstPoint) {
System.out.println("You win!"); //IF THE VALUE IS THE SAME AS YOUR FIRST VALUE YOU WIN
} else {
System.out.print("Error. Please try again.");
}
//KEEP ROLLING UNTIL YOU HAVE A WIN/LOSE
}
}
return true;
}
public static void main(String[] args) {
System.out.println("How many rounds would you like to play?");
int amtRound = in.nextInt();
for (int i=1; i<=amtRound; i++) {
boolean result = round();
}
}
}
当我运行代码时,它确实会问我想玩多少轮,尽管它会跳过整个过程,只是跳转到 while
循环并打印 System.out .print("错误,请重试。");
最佳答案
while
循环被锁定
由于您从未更改secPhase
,因此它永远不会停止。您可能想在循环中的某个位置执行类似 secPhase = roll(); 的操作。
此外,您不应该这样做 int secPhase = firstPoint;秒相位 = 0;
。只需执行int secPhase = 0;
即可。
此外,我认为您甚至不需要 while
循环来开始。
关于Java 掷骰子程序(掷骰子游戏)未获得输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964907/