计数器未递增 Java
Counter not incrementing in Java
我创建了一个名为 attemptCount 的变量,我在 0 处启动它,每次玩家尝试输入不可用的方向时都应该递增。我 运行 调试器和变量重置为 0 一旦我尝试再次键入相同的方向。变量 attemptCount 未在本地声明,而是在游戏方法中设置。任何想法为什么计数器重置而不是递增?我尝试将 attempCount 设置为 static 并尝试创建一个新变量来增加 attemptCount 例如counter = attempCounter++ 但 none 有效。我还查看了另一个已回答的问题,但无法理解在这种情况下我将如何应用它。任何人都可以阐明我做错了什么吗?
public class Game implements java.io.Serializable {
private final String resourcePath;
private final ClassLoader cl;
private World world;
private List<Ghost> ghosts = new ArrayList<>();
private List<MiniGhost> miniGhosts = new ArrayList<>();
private Map<String, List<? extends Items>> items = new HashMap<>();
private List<Weapon> weapons;
private final SaveGame SaveGame = new SaveGame();
private Ghost currentGhost;
private final Random r = new Random();
private final String divider = "***************************************" +
"*********************************************************";
private Player player;
private HauntingJFrame jFrame;
SaveGame save = new SaveGame();
private final transient FileReader fileReader = new FileReader();
private MusicPlayer mp;
private MusicPlayer soundEffect;
private MusicPlayer walkEffect;
private MusicPlayer keyboardEffect;
private MusicPlayer paperFalling;
private int guessCounter = 0;
boolean isGameRunning = true;
String moveGuide = "To move type: Go North, Go East, Go South, or Go West";
String currentRoom;
private String currentLoc;
private boolean isSound = true;
private int attemptCount;
public void changeRoom(boolean isValidInput, String[] input, int attemptCount) throws
IOException, InterruptedException {
while (isValidInput) {
String normalize = normalizeText(input[1]);
try {
if (world.getCurrentRoom().roomExits.containsKey(normalize)) {
player.setMostRecentExit(normalize);
world.setCurrentRoom(world.getCurrentRoom().roomExits.get(normalize));
isValidInput = false;
if (isSound) {
walkEffect.playSoundEffect();
}
Thread.sleep(1800);
narrateRooms(world.getCurrentRoom().getDescription(), Color.red);
updateCurrentRoom();
} else {
replaceGameWindowWithColorText("You hit a wall. Try again.\n ", Color.RED);
attemptCount++;
System.out.println(attemptCount);
if (attemptCount >= 2) {
appendToGameWindowsWithColorNoSound("\n", Color.WHITE);
openMap();
appendToGameWindowsWithColorNoSound("Where would you like to go?\n ", Color.WHITE);
setAttemptCount(0);
}
}
break;
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
您在 changeRoom
中有一个参数也名为 attemptCount
。当您在方法体中引用 attemptCount
时,您指的是参数而不是成员变量。您可以通过更改参数名称、更改变量名称或在您指的是成员变量而不是参数时使用 this.attemptCount
来修复它。
“变量 attemptCount 未在本地声明”
实际上是,作为方法参数:
public void changeRoom(boolean isValidInput, String[] input, int attemptCount)
因此,即使在本地递增,它的值也不会返回给调用代码,因为它没有作为引用传递 - 参见:Is Java “pass-by-reference” or “pass-by-value”?
我创建了一个名为 attemptCount 的变量,我在 0 处启动它,每次玩家尝试输入不可用的方向时都应该递增。我 运行 调试器和变量重置为 0 一旦我尝试再次键入相同的方向。变量 attemptCount 未在本地声明,而是在游戏方法中设置。任何想法为什么计数器重置而不是递增?我尝试将 attempCount 设置为 static 并尝试创建一个新变量来增加 attemptCount 例如counter = attempCounter++ 但 none 有效。我还查看了另一个已回答的问题,但无法理解在这种情况下我将如何应用它。任何人都可以阐明我做错了什么吗?
public class Game implements java.io.Serializable {
private final String resourcePath;
private final ClassLoader cl;
private World world;
private List<Ghost> ghosts = new ArrayList<>();
private List<MiniGhost> miniGhosts = new ArrayList<>();
private Map<String, List<? extends Items>> items = new HashMap<>();
private List<Weapon> weapons;
private final SaveGame SaveGame = new SaveGame();
private Ghost currentGhost;
private final Random r = new Random();
private final String divider = "***************************************" +
"*********************************************************";
private Player player;
private HauntingJFrame jFrame;
SaveGame save = new SaveGame();
private final transient FileReader fileReader = new FileReader();
private MusicPlayer mp;
private MusicPlayer soundEffect;
private MusicPlayer walkEffect;
private MusicPlayer keyboardEffect;
private MusicPlayer paperFalling;
private int guessCounter = 0;
boolean isGameRunning = true;
String moveGuide = "To move type: Go North, Go East, Go South, or Go West";
String currentRoom;
private String currentLoc;
private boolean isSound = true;
private int attemptCount;
public void changeRoom(boolean isValidInput, String[] input, int attemptCount) throws
IOException, InterruptedException {
while (isValidInput) {
String normalize = normalizeText(input[1]);
try {
if (world.getCurrentRoom().roomExits.containsKey(normalize)) {
player.setMostRecentExit(normalize);
world.setCurrentRoom(world.getCurrentRoom().roomExits.get(normalize));
isValidInput = false;
if (isSound) {
walkEffect.playSoundEffect();
}
Thread.sleep(1800);
narrateRooms(world.getCurrentRoom().getDescription(), Color.red);
updateCurrentRoom();
} else {
replaceGameWindowWithColorText("You hit a wall. Try again.\n ", Color.RED);
attemptCount++;
System.out.println(attemptCount);
if (attemptCount >= 2) {
appendToGameWindowsWithColorNoSound("\n", Color.WHITE);
openMap();
appendToGameWindowsWithColorNoSound("Where would you like to go?\n ", Color.WHITE);
setAttemptCount(0);
}
}
break;
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
您在 changeRoom
中有一个参数也名为 attemptCount
。当您在方法体中引用 attemptCount
时,您指的是参数而不是成员变量。您可以通过更改参数名称、更改变量名称或在您指的是成员变量而不是参数时使用 this.attemptCount
来修复它。
“变量 attemptCount 未在本地声明”
实际上是,作为方法参数:
public void changeRoom(boolean isValidInput, String[] input, int attemptCount)
因此,即使在本地递增,它的值也不会返回给调用代码,因为它没有作为引用传递 - 参见:Is Java “pass-by-reference” or “pass-by-value”?