下面是我的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”和其他意外结果之前。最好在所有集合和其他通用类型类中定义类型。
麻烦在于得分的类型。您正在将乐谱作为字符串插入。第二个小问题是排序顺序应该颠倒。
请看以下示例:
public class Test {
public static void main(String[] args) throws IOException {
System.out.println("String scores:");
HashMap<String, String> scores = new HashMap<>();
scores.put("12", "John");
scores.put("240", "Mary");
scores.put("14", "Sean");
scores.put("35", "Pat");
sort(scores);
System.out.println();
System.out.println("Integer scores:");
HashMap<Integer,String> integerScores = new HashMap<>();
integerScores.put(12, "John");
integerScores.put(240, "Mary");
integerScores.put(14, "Sean");
integerScores.put(35, "Pat");
sort(integerScores);
}
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>(Collections.reverseOrder()); // Descending
map.putAll(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());
}
}
}
此示例在'scores'和'integerScores'上使用您的排序方法。这是排序后的输出:字符串分数:35:拍240:玛丽14:肖恩12:约翰
整数分数:240:玛丽35:拍14:肖恩12:约翰
您可以在第一个排序中看到35在240之前,因为3在2之前。它在进行字符串排序。第二种是按整数值排序。一旦了解了这一点,就很容易修复您的代码。
原始代码将键作为字符串插入:
highscoreTable.put(key, value);
密钥应为整数:
highscoreTable.put(Integer.valueOf(key), value);
还请注意,提供的答案可以轻松运行。它包括数据(比附加文件更简单)和输出。以这种方式格式化问题时,更容易获得帮助。