Java掷骰子程序(掷骰子游戏)没有得到输出



我是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("Error. Please try again.");

while循环被锁定

既然你永远不会改变secPhase,它永远不会停止。您可能想在循环中的某个地方做一些类似secPhase = roll();的事情。

另外,你不应该做int secPhase = firstPoint; secPhase = 0;.只要做int secPhase = 0;.

另外,我认为您甚至不需要while循环。

由于您是从 main 循环调用它的 - 您在 round 中的else情况应该简单得多:

public static Random rand = new Random(); //INITIALIZE RANDOM
public static Scanner in = new Scanner(System.in); //INITIALIZE USER INPUT
public static int numOfDice = 2; //INITIALIZE NUMBER OF DICES INVOLVED
public static int numOfSides = 6; //INITIALIZE NUMBER OF SIDES INVOLVED
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 void 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 {
        System.out.println("you tied!");
    }
}
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++) {
        round();
    }
}

此外,您可以在声明时初始化变量,并且不需要round()返回任何值,因为无论如何您都不会对它执行任何操作。

示例输出:

How many rounds would you like to play?
7
you tied!
you tied!
you tied!
you tied!
you tied!
You win!
You lose!

实现它的另一种方法是将amtRound传递给round,并让它计算出它应该在一个循环中运行多少次(迭代):

public static void round(int times) {
    while (times > 0) {
        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 {
            System.out.println("you tied!");
        }
        times--;
    }
}
public static void main(String[] args) {
    System.out.println("How many rounds would you like to play?");
    int amtRound = in.nextInt();
    round(amtRound);
}

最新更新