【发布时间】:2017-05-28 06:50:03
【问题描述】:
所以,我能够让我的代码运行,但是我在高分代码方面遇到了问题。我无法使用 bufferedreader 和 printwriter 函数,因为出于某种我不理解的原因,它们没有运行。我希望程序将分数与高分进行比较,如果分数大于高分,高分将在 txt 文件中更新。之所以需要 txt 文件,是因为一旦程序关闭,我需要一种方法来检查以前的高分。我对使用程序处理、写入和读取 txt 文件真的很陌生,我看过的其他网站和论坛都没有帮助,因为他们没有将高分变量写入 txt 文件。请帮助,我准备打破我的电脑。 EM1.score = 在程序过程中累积的分数。
class High_Score {
int highscore = 0;
PrintWriter output; //imports the writer
BufferedReader reader; //imports the reader
int state = 0; //sets the varoable for the keyboard
void scoring() {
int score = EM1.score;
if (score > highscore) {
highscore = score;
}
textSize(30);
text("Your score is "+ score, 150, height/4);
text("The current highscore is "+highscore+".", 75, height/2);
text("Try to beat it.", 200, 450);
textSize(12);
text("Press esc to exit this page.", 225, 550);
}
void reader() {
importHS();
updateHS();
}
void updateHS() {
int score = EM1.score;
output = createWriter("highscore.txt"); //creates the file that will be
if (highscore < score) {
highscore = score;
output.print(highscore);
output.close();
}
}
void importHS() {
reader = createReader("highscore.txt"); //reads the current highscore
if (reader == null) {
highscore = 0;
return;
}
String line;
try {
line = reader.readLine();
}
catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line != null) {
highscore = int(line);
println(highscore);
}
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
void type() { //allows the screen to close is esc is pressed on the keyboard
state = key;
if (key == ESC) {
exit();
}
}
}
【问题讨论】:
标签: processing bufferedreader filewriter