gpt4 book ai didi

java - 排序功能不排序! java

转载 作者:行者123 更新时间:2023-12-01 20:05:28 24 4
gpt4 key购买 nike

下面是我的 Java 测验程序中的代码块。我正在尝试将高分读取/写入文件并对高分表进行排序。

public static void main(String[] args) throws IOException {

sort(highscoreTable);
int score1 = 0;
int score2 = 0;
int totalscore = 0;
int NumberofQuestions = 5;

// RECORDS TO STORE QUESTION, ANSWER WOULD BE HERE //

private static void start(int NumberofQuestions, String[] Answers, String[][] questions, int score1, int score2, int totalscore) throws IOException {
// DISPLAYED ON THE HOME SCREEN SO USER CAN CHOOSE WHAT THEY WANT TO DO
System.out.println("[0] Leave\n");
while(true) {
System.out.println("[1] Play");
System.out.println("[2] Highscore");
System.out.print("Enter Choice: ");
String useranswer = scanner.nextLine();
System.out.println();
if (useranswer.equals("1")) {
mainLoop(NumberofQuestions, Answers, questions, score1, score2, totalscore);
} else if (useranswer.equals("2")) {
sort(highscoreTable);
} else if (useranswer.equals("0")) {
try {
save();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}

我希望这个位首先显示在屏幕上,如果用户按2,我想编程从文件中读取并显示之前的高分

 public static void save() throws IOException {

String aggFileName = "agg-"+String.valueOf("06.txt");
FileWriter fstream = new FileWriter(aggFileName);
BufferedWriter out = new BufferedWriter(fstream);

for (Map.Entry<String, String> entry : highscoreTable.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); //this statement prints out my keys and values
out.write(entry.getKey() + "--" + entry.getValue() + "\n");
System.out.println("Done");
out.flush(); // Flush the buffer and write all changes to the disk
}

out.close(); // Close the file
}

保存方法工作完美,我没有任何问题。

public static void mainLoop(int NumberofQuestions, String[]Answers, String[][] questions, int score1, int score2, int totalscore) throws IOException {

// USER WOULD ANSWER QUESTIONS HERE
addHighscore(name, totalscore);
}

public static void addHighscore(String name, int totalscore) throws IOException {
highscoreTable.put(String.valueOf(totalscore), name);
}

此处的函数将用户名和总分添加到树形图中

public static void highscoreImport(HashMap highscoreTable) throws IOException {

String filePath = "agg-06.txt";
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
while ((line = reader.readLine()) != null)
{
String[] parts = line.split("--", 2);
if (parts.length >= 2)
{
String key = parts[0];
String value = parts[1];
highscoreTable.put(key, value);
} else {
}
}
for (Object key : highscoreTable.keySet())
{
System.out.println(key + "--" + highscoreTable.get(key));
}

reader.close();

}

这是我遇到问题的部分。我希望程序从文件中获取信息,现在将其与来自用户最近测验的数据合并,然后对分数进行排序(我想要一个高分表),以便当用户输入“2”时查看高分表,会按照降序排列

public static void sort(HashMap highscoreTable) throws IOException {

System.out.println("Before Sorting:");
Set set = highscoreTable.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry me = (Map.Entry)iterator.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
Map<Integer, String> map = new TreeMap<Integer, String>(highscoreTable);
System.out.println("After Sorting:");
Set set2 = map.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry me2 = (Map.Entry)iterator2.next();
System.out.print(me2.getKey() + ": ");
System.out.println(me2.getValue());
}
}

这里,排序“之前”和“之后”输出的列表是同一个未排序的列表

<小时/>

抱歉,阅读时间太长,如果您能提供帮助或指点来解决此问题,我将不胜感激。

最佳答案

自从在addHighscore你正在做的方法:

highscoreTable.put(String.valueOf(totalscore), name);

我认为它是 Map<String, String> (键和值类型均为 String )。

但是在 sort你这样做的方法...

Map<Integer, String> map = new TreeMap<Integer, String>(highscoreTable);

如果 highscoreTable 的类型被正确定义,TreeMap实例化应该在编译时失败。如果没有,则作为构造函数TreeMap(Map)按其键的自然顺序排序(请参阅 Javadoc),它可能按 String 排序。订单或其他。因此“111”将位于“12”和其他意外结果之前。在所有集合和其他泛型类型类中定义类型是一个很好的做法。

关于java - 排序功能不排序! java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986274/

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