private int score = 0;
private int highScore = 0;
private void gameOver(Graphics g) {
try{
PrintWriter writer = new PrintWriter(new FileWriter("C:\\Users\\Videvik\\workspace\\madu\\logi1.txt", true));
writer.println(score);
writer.close();
} catch(Exception ex){ex.printStackTrace();}//creates .txt file
File file = new File("logi1.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) // read the score file line by line
{
try {
int score2 = Integer.parseInt(line.trim()); // parse each line as an int
if (score2 > highScore) // and keep track of the largest
{
highScore = score;
}
} catch (NumberFormatException e1) {
// ignore invalid scores
//System.err.println("ignoring invalid score: " + line);
}
line = reader.readLine();
}
reader.close();
} catch (IOException ex) {
System.err.println("ERROR reading scores from file");
String msg = "Game over!";//works
String msg2 = "Points: "+score;//works
String msg3 = "MaxPoints: "+highScore;//does not work
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.red);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg))/2, B_HEIGHT/2);
g.drawString(msg2, (B_WIDTH - metr.stringWidth(msg2))/4, B_HEIGHT/4);
g.drawString(msg3, (B_WIDTH - metr.stringWidth(msg3))/6, B_HEIGHT/6);
}
}
Ну, моя проблема в том, что я не могу распечатать максимальный балл на экране из TXT-файла. Первая программа создает файл .txt, где сохраняются все баллы (он работает). Вторая функция после этого должна найти самый высокий балл оттуда и вытащить его (не работает). (Он рисует «Точки» и «Игра поверх!»). Eclipse не показывает ошибок в коде. Что я делаю неправильно? Я должен исправить эту вещь на следующую среду или я не экзамен лолПолучение рекорда для змеиной игры из .txt файла
Пожалуйста, покажите только соответствующие части вашего кода - это довольно сложно, когда вопрос имеет это много строк. Рассмотрите редактирование, чтобы сделать вопрос более кратким. –
Да, по моей вине, я снял несущественные детали. – Hidalgo