gpt4 book ai didi

java - 在 Java 中更新参数值

转载 作者:行者123 更新时间:2023-12-02 00:08:47 25 4
gpt4 key购买 nike

我不太确定该标题是什么,所以我提前道歉。我目前正在尝试制作一款游戏,总分必须加起来达到 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 方法调用?请记住,这是一个递归调用。

关于java - 在 Java 中更新参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com