首页 > 解决方案 > 尝试在处理游戏时打印高分并将其保存到外部文件中以再次读取

问题描述

我现在正在用java做一个项目,基本上就是游戏蛇。我通过将高分打印到外部 txt 文件来完成它,以便在高分被打破时可以读取和写入它们。似乎不想使用我从文件中读取的行的解析。我需要成为一个 int 以便我可以将它与最近的分数进行比较。

BufferedReader reader;
PrintWriter output;
String line = "1";

reader = createReader("SnakeFile.txt");
output = createWriter("SnakeFile.txt");

try {
    line = reader.readLine();
    boolean t = true;
    while (t) {
        String score = line;
        int x = Integer.parseInt(score);
        if (x < snake.bodyLength) {
            System.out.println("New High Score" + snake.bodyLength);
            output.println(snake.bodyLength);
        }
        // line = reader.readLine();
        t = false;
    }
    // highScore = reader.readLine();
    // highScore ="5";
    // output.flush(); // Writes the remaining data to the file
    output.close(); // Finishes the file

} catch (IOException e) {
    e.printStackTrace();
    line = null;
}

标签: java

解决方案


推荐阅读