繁体   English   中英

如何在存储前 10 名得分和得分球员姓名的 txt 文件上创建“高分列表”?

[英]How can I create a "high score list" on a txt file which stores top 10 scores and names of the players who scored them?

我需要在 txt 文件中制作高分列表。 在第一个游戏中,txt 文件应该是空的,因为它是第一个游戏。 第一场比赛结束后,得分列表必须每次都用球员的名字和球员的得分进行更新。 名单当然要根据玩家的得分从高到低排序。 10 场比赛后,应删除最后一场比赛,列表中应仅保留 10 场比赛。

我正在尝试这样做,但每次我的 txt 文件都为空。 我该如何解决这个问题?

我的高分 class:

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Formatter;
import java.nio.file.Paths;

public class HighScore {

    public class HighScoreEntry {
        private String name;
        private int score;

        public HighScoreEntry(String name, int score) {
            this.name = name;
            this.score = score;
        }

        public String getName() {
            return name;
        }

        public int getScore() {
            return score;
        }
    }

    public void writeHighScores(HighScoreEntry[] highScores) {
        Formatter f = null;
        FileWriter fw = null;
        try {
            fw = new FileWriter("highscores.txt",true);
            f = new Formatter(fw);
            for (int i = 0; i < highScores.length; i++) {
                f.format("%s:%d%n", highScores[i].getName(), highScores[i].getScore());
            }
        } catch (IOException e) {
            System.out.println("An error occurred while writing the high scores file.");
        } finally {
            if (f != null) {
                f.close();
            }
        }
    }
    
    public HighScoreEntry[] readHighScores() {
        HighScoreEntry[] highScores = new HighScoreEntry[10];
    
        // Initialize the high scores array with default values
        for (int i = 0; i < highScores.length; i++) {
            highScores[i] = new HighScoreEntry("", 0);
        }
        
        Scanner reader = null;

        try {
            reader = new Scanner(Paths.get("highscores.txt"));
            int i = 0;
            while (reader.hasNextLine() && i < 10) {
                String line = reader.nextLine();
                String[] parts = line.split(":");
                String name = parts[0];
                int score = Integer.parseInt(parts[1]);
                highScores[i] = new HighScoreEntry(name, score);
                i++;
            }
        } catch (IOException e) {
            System.out.println("An error occurred while reading the high scores file.");
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    
        return highScores;
    }

    public void updateHighScores(String name, int score) {
        System.out.println("Updating high scores with name " + name + " and score " + score);
        // Write the player's score and name to the high scores file
        writeHighScores(new HighScoreEntry[] {new HighScoreEntry(name, score)});
        
        // Read the high scores from the file
        HighScoreEntry[] highScores = readHighScores();
        
        // Sort the high scores
        sortHighScores(highScores);
    }    

    private void sortHighScores(HighScoreEntry[] highScores) {
        for (int i = 0; i < highScores.length - 1; i++) {
            for (int j = i + 1; j < highScores.length; j++) {
                if (highScores[i].getScore() < highScores[j].getScore()) {
                    HighScoreEntry temp = highScores[i];
                    highScores[i] = highScores[j];
                    highScores[j] = temp;
                }
            }
        }
    }
}

我在游戏class中的调用方式:

HighScore highScore = new HighScore();
highScore.updateHighScores(user, playerPoints);

我只需要使用它们。 除了这些我什么也用不了。

Formatter class 返回格式化的字符串,但您没有捕获format调用的返回值,也没有将结果字符串写入 FileWriter。

它应该看起来像这样:

String result = f.format("%s:%d%n", highScores[i].getName(), highScores[i].getScore());
fw.write(string);

给或换行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM