嘿伙计们,在过去的几个小时里,我一直在努力弄清楚如何读取和写入高分数到txt文件。我不断收到代码中的错误,当我修复它们时会出现更多问题。我一直在尝试使用BufferedReaders和BufferedWriters,但有些尝试从未解决过。
顺便说一句,如果高分数等级("")我调用GetHighScore类,并且当玩家死亡时我调用CheckHighScore。
以下是我现在无法使用的代码:
public static int score = 0;
public static String highScore = "0";
public static String GetHighScore() {
// I call this in my player class when highScore.equals("");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("res/Files/highscore.txt"));
return reader.readLine();
} catch (Exception e) {
return "Make One";
} finally {
try {
if (reader != null)
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void CheckHighScore() {
// I call this when i die to check the score
if (score > Integer.parseInt(highScore)) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(
"res/Files/highscore.txt"));
writer.write(score);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是出现的错误:
Thread [Thread-3] (Suspended (exception NullPointerException))
Player.tick() line: 123
Game.tick() line: 127
Game.run() line: 79
Thread.run() line: not available