GameContinue() 未运行。应该循环回到 main() 的开头

GameContinue() not functioning. should loop back to beginning of main()

我目前正在制作一个小猪游戏,大部分功能都可以正常运行。 如果玩家 1 决定停止滚动,而玩家 2 决定停止滚动,我需要代码工作,游戏将循环返回,直到一个玩家达到 20 分。目前,代码在 1 运行 through.

之后结束
import java.util.Random;
import java.util.Scanner;
public class Project3_Part1_Submit {
    static int playerScore = 0;
    static int playerTotal = 0;
    static int dice = 0;
    static final int FINAL_SCORE = 20;
    static int playerTwoScore = 0;
    static int playerTwoTotal = 0;
    static boolean gameOver = false;
    static boolean turnOver = false;
    static char repeat;
    static String input;
    static Scanner key = new Scanner(System.in);

    static Random rand = new Random();
    
    public static void main(String[] args) {
        PlayerOneTurn();
        PlayerTwoTurn();
        GameContinue();
        if (playerTotal >= FINAL_SCORE) {
            System.out.print("Current Score: Player 1 has " + playerTotal);
            System.out.print(", Player 2 has " + playerTwoTotal);
            System.out.println("");
            System.out.println("Player 1 wins!");
        }
        if (playerTwoTotal >= FINAL_SCORE) {
            System.out.print("Current Score: Player 1 has " + playerTotal);
            System.out.print(", Player 2 has " + playerTwoTotal);
            System.out.println("");
            System.out.println("Player 2 wins!");
        }
    }
    
    public static void PlayerOneTurn() {
        while (turnOver == false) {
            do {
                System.out.print("Player 1 turn total is " + playerTotal);       // welcome line
                System.out.print(". Enter (r)oll or (s)top: ");
                System.out.println("");
                
                input = key.next();                                              // input = next key entered
                repeat = input.charAt(0);
                
                if (repeat == 'r')                                               // if input letter = r
                {
                    dice = rand.nextInt(3) + 1;                                  // set dice to number between 1-6
                    System.out.println("Player 1 rolled: " + dice);              // display number, signifying the amount rolled
                    
                    if (dice == 1)                                               // if the number rolled happens to be 1...
                    {
                        playerScore = 0;                                         // reset score to 0
                        System.out.print("Player 1 lost their turn! ");
                        System.out.print("Player 1 total is " + playerTotal);
                        System.out.println("");
                        return;
                    }
                    else 
                    {
                        playerScore += dice;                                      // add dice amount to player score
                        
                        System.out.print("Player 1 turn total is " + playerScore);// print out total amount earned 
                        System.out.print(" Enter (r)oll or (s)top: ");             // repeat question
                        System.out.println("");
                        input = key.next();
                        repeat = input.charAt(0);
                        if (playerScore >= 20) {
                            playerTotal = playerScore;
                            gameOver = true;
                            return; 
                        }
                        
                    }
                
                }
                else if (repeat == 's')                                           // if neither option is called
                {
                    return;    
                }
                else 
                {
                    System.out.println("Incorrect entry, please try again");       // prompt retry
                    System.out.println("");
                    input = key.next();
                }
            }while(turnOver == false || dice != 1);
            
            
            playerTotal += playerScore;
            playerScore = 0;
            if (playerTotal >= FINAL_SCORE) {
                // System.out.println("YOU WIN!");
                gameOver = true;
                while (playerTotal >= FINAL_SCORE)
                    ;

                break;
            }
        }       
    }
    
    public static void PlayerTwoTurn() {
        System.out.println("success");
        while (turnOver == false) {
            do {
                System.out.print("Player 2 turn total is " + playerTwoTotal);       // welcome line
                System.out.print(". Enter (r)oll or (s)top: ");
                System.out.println("");
                input = key.next();                                              // input = next key entered
                repeat = input.charAt(0);
                
                if (repeat == 'r')                                               // if input letter = r
                {
                    dice = rand.nextInt(3) + 1;                                  // set dice to number between 1-6
                    System.out.println("Player 2 rolled: " + dice);              // display number, signifying the amount rolled
                    System.out.println("");
                    
                    if (dice == 1)                                               // if the number rolled happens to be 1...
                    {
                        playerTwoScore = 0;                                         // reset score to 0
                        System.out.print("Player 2 lost their turn! ");
                        System.out.print("Player 2 total is " + playerTwoTotal);
                        System.out.println("");
                        return;
                    }
                    else 
                    {
                        playerTwoScore += dice;                                      // add dice amount to player score
                        
                        System.out.print("Player 2 turn total is " + playerTwoScore);// print out total amount earned 
                        System.out.print(" Enter (r)oll or (s)top: ");            // repeat question.
                        System.out.println("");
                        input = key.next();
                        repeat = input.charAt(0);
                        if (playerTwoScore >= 20) {
                            playerTwoTotal = playerTwoScore;
                            gameOver = true;
                            return; 
                        }
                        
                    }
                
                }
                else if (repeat == 's')                                           // if neither option is called
                {
                    turnOver = true;  
                    return; 
                }
                else 
                {
                    System.out.println("Incorrect entry, please try again");       // prompt retry
                    System.out.println("");
                    input = key.next();
                }
            }while(turnOver == false || dice != 1);
            
            
            playerTwoTotal += playerTwoScore;
            playerTwoScore = 0;
            if (playerTwoTotal >= FINAL_SCORE) {
                // System.out.println("YOU WIN!");
                gameOver = true;
                while (playerTwoTotal >= FINAL_SCORE)
                    ;

                break;
            }
        }
    }
    public static void GameContinue() {
        if (playerTotal < 20 && playerTwoTotal < 20) {                          //if neither players totals or 20 or greater
            PlayerOneTurn(); //go back to playerOne
        }
        else //if either one is 20 or greater
        {
            return; //go to main method and finish
        }
    }
    
}

我创建的 GameContinue() 函数是为了循环回到 PlayerOne。

public static void GameContinue() {
        if (playerTotal < 20 && playerTwoTotal < 20) { //if neither players totals or 20 or greater
            gameOver = false;
            PlayerOneTurn(); //go back to playerOne
        }
        else //if either one is 20 or greater
        {
            return; //go to main method and finish
        }
    }

我知道调用 PlayerOneTurn();不会将其循环回 main 方法的开头。我试过像这样再次调用所有三种方法。

public static void GameContinue() {
        if (playerTotal < 20 && playerTwoTotal < 20) { //if neither players totals or 20 or greater
            gameOver = false;
            PlayerOneTurn(); //go back to playerOne
            PlayerTwoTurn();
            GameContinue();
        }
        else //if either one is 20 or greater
        {
            return; //go to main method and finish
        }
    }

显然这只是导致了一大堆错误。我应该如何让我的代码继续循环直到满足 GameContinue() 中的 if 语句?

正如 Ecto 评论的那样,您可以在主函数中使用 while 循环。此处显示:

public static void main(String[] args) {
    while (GameContinue()) {
        PlayerOneTurn();
        PlayerTwoTurn();
    }
    if (playerTotal >= FINAL_SCORE) {
        System.out.print("Current Score: Player 1 has " + playerTotal);
        System.out.print(", Player 2 has " + playerTwoTotal);
        System.out.println("");
        System.out.println("Player 1 wins!");
    }
    if (playerTwoTotal >= FINAL_SCORE) {
        System.out.print("Current Score: Player 1 has " + playerTotal);
        System.out.print(", Player 2 has " + playerTwoTotal);
        System.out.println("");
        System.out.println("Player 2 wins!");
    }
}

现在显然这是一个错误,因为 GameContinue() 是一个 void 方法。为了解决这个问题,您可以将 GameContinue() 更改为:

public static boolean GameContinue() {
    if (playerTotal < 20 && playerTwoTotal < 20) {
        gameOver = false;
        return true;
    } else {
        return false;
    }
}

这将解决您的问题,并且您的代码几乎没有改动。