-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighScore.java
63 lines (53 loc) · 1.25 KB
/
HighScore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class HighScore extends JFrame {
private static final long serialVersionUID = 1L;
public static int highScore;
JLabel scoreLabel;
public static void main(String[] args) {
new HighScore();
}
public HighScore() {
super("Score");
loadScore();
}
public HighScore(int score) {
this();
}
public static int getScore(int score)
{
loadScore();
addScore(score);
return highScore;
}
public static void addScore(int newScore) {
if (newScore > highScore) {
highScore = newScore;
}
saveScore();
}
public static void saveScore() {
try {
PrintWriter out = new PrintWriter("highscore.txt");
out.println(highScore);
out.close();
} catch (IOException e) {
System.out.println("Cannot open file (saveScore).");
}
}
public static void loadScore() {
String temp;
try {
BufferedReader in = new BufferedReader(new FileReader("highscore.txt"));
temp = in.readLine(); // Read a line
highScore = Integer.parseInt(temp);
in.close();
} catch (IOException e) {
System.out.println("Cannot open file (highscore.txt), file has been created.");
}
}
}