public void action() throws Exception{ //让地面动起来 MouseListener l=new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { //鼠标按下 // brid.flappy(); //鸟向上飞 try { switch (state) { case GAME_OVER: column1 = new Column(1); column2 = new Column(2); brid = new Brid(); score = 0; state = START; break; case START: state = RUNNING; case RUNNING: brid.flappy(); } }catch (Exception ignored){ 根据以上代码添加分数排行,并以文件形式保存 }、
时间: 2024-02-10 19:19:59 浏览: 76
java.lang.Void类的解析与使用详解
根据以上代码添加分数排行,并以文件形式保存的话,可以在catch块中添加以下代码:
```
try {
// 读取之前保存的分数排行
List<Integer> scoreList = new ArrayList<>();
File scoreFile = new File("score.txt");
if (scoreFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(scoreFile));
String line;
while ((line = reader.readLine()) != null) {
scoreList.add(Integer.parseInt(line));
}
reader.close();
}
// 添加当前分数到排行榜中
scoreList.add(score);
Collections.sort(scoreList, Collections.reverseOrder());
// 保存排行榜到文件中
BufferedWriter writer = new BufferedWriter(new FileWriter(scoreFile));
for (int i = 0; i < scoreList.size() && i < 10; i++) {
writer.write(scoreList.get(i) + "\n");
}
writer.close();
} catch (Exception ignored) {
}
```
这段代码会将当前分数添加到排行榜中,并按照从大到小的顺序排序,最后保存排行榜到文件中。排行榜文件名为score.txt,每行一个分数,最多保存10个分数。如果文件不存在,则会创建新的文件。
阅读全文