我不太确定该用什么标题,所以我事先表示歉意。我目前正在尝试制作一款游戏,该游戏的总积分必须达到100,玩家才能获胜。我遇到了这样的问题:在PlayerTurn方法返回PlayerTotalPoints的数量并将其设置为PlayerTotalPoints中的新值之后,“ PlayerTotalPoints”没有更新,以便再次在该方法中重新运行。

这是我的代码:

public static void main(String[] args) {
        int PlayerTotalPoints = 0;
        int ComputerTotalPoints = 0;
        while (!IsGameOver(PlayerTotalPoints, ComputerTotalPoints)) {
            int TurnPointCounter = 0;
            System.out.println("It is the player's turn!");
            PlayerTotalPoints = PlayerTurn(TurnPointCounter, PlayerTotalPoints);
            System.out.println("The player currently has " + PlayerTotalPoints + " total points!");
            System.out.println("It is the computer's turn!");
            ComputerTotalPoints = ComputerTurn(TurnPointCounter, ComputerTotalPoints);
            System.out.println("The computer currently has " + ComputerTotalPoints + " total points!");
        }
    }


PlayerTurn方法返回PlayerTotalPoints的新值(我检查并返回了正确的PlayerTotalPoints值),但由于某种原因,该值并未传递到PlayerTotalPoints变量中,该变量需要随着玩家累积分数而继续保持下去。这是一项作业,因此我不确定我可以在线共享多少代码,但是如果您需要更多代码示例,请告诉我,谢谢。

编辑:这些是PlayerTurn和ComputerTurn方法:

    public static int PlayerTurn(int counter, int PlayerTotalPoints) {
        System.out.println("The player currently has " + counter + " turn points.");
        System.out.println("Do you want to play or hold this turn? (P/H)");
        String answer = scan.nextLine();
        answer = answer.replaceAll(" ", "");
        answer = answer.toUpperCase();
        if (answer.equals("H")) {
            PlayerTotalPoints += counter;
            System.out.println("You have elected to hold this turn.");
//            System.out.println("You currently have " + PlayerTotalPoints + " total points.");
            System.out.println(PlayerTotalPoints);
            return PlayerTotalPoints;
        }
        int[] OneRoll = RollDice();
        int TurnCount = Turn(OneRoll, counter);
        if (OneRoll[0] == 6 && OneRoll[1] == 6) {
            PlayerTotalPoints = 0;
            return PlayerTotalPoints;
        }
        if (OneRoll[0] == 6 | OneRoll[1] == 6) {
//            System.out.println("The player currently has " + PlayerTotalPoints + " total points.");
            return PlayerTotalPoints;
        }
        PlayerTurn(TurnCount, PlayerTotalPoints);
        return PlayerTotalPoints;
    }

    public static int ComputerTurn(int counter, int ComputerTotalPoints) {
        System.out.println("The computer currently has " + counter + " turn points.");
        int n = generator.nextInt(100);
        if (n >= 70) {
            ComputerTotalPoints += counter;
            System.out.println("The computer has decided to hold this turn. ");
//            System.out.println("The computer currently has " + ComputerTotalPoints + " total points.");
            return ComputerTotalPoints;
        }
        int[] CompRoll = RollDice();
        int CompTurnCount = Turn(CompRoll, counter);
        if (CompRoll[0] == 6 && CompRoll[1] == 6) {
            ComputerTotalPoints = 0;
            return ComputerTotalPoints;
        }
        if (CompRoll[0] == 6 | CompRoll[1] == 6) {
//            System.out.println("The computer currently has " + ComputerTotalPoints + " total points.");
            return ComputerTotalPoints;
        }
        ComputerTurn(CompTurnCount, ComputerTotalPoints);
        return ComputerTotalPoints;
    }

最佳答案

以下内容不应该:

    PlayerTurn(TurnCount, PlayerTotalPoints);
    return PlayerTotalPoints;




    PlayerTotalPoints = PlayerTurn(TurnCount, PlayerTotalPoints);
    return PlayerTotalPoints;


对于PlayerTurn和ComputerTurn方法调用?请记住,这是一个递归调用。

10-07 20:13