Processing 在处理PrintWriter和BufferedReader时遇到困难
所以,我可以让我的代码运行,但我有麻烦的高分代码。我无法使用bufferedreader和printwriter函数,因为我不理解某些原因,它们没有运行。我希望程序将分数与highscore进行比较,如果分数大于highscore,highscore将在txt文件中更新。txt文件之所以是必需的,是因为一旦程序关闭,我需要一种方法来检查以前的高分。我对使用程序处理、写入和读取txt文件非常陌生,我所看到的其他网站和论坛都没有帮助,因为它们没有将highscore变量写入txt文件。请帮帮我,我准备把电脑弄坏了。 EM1.score=课程过程中累积的分数Processing 在处理PrintWriter和BufferedReader时遇到困难,processing,bufferedreader,filewriter,Processing,Bufferedreader,Filewriter,所以,我可以让我的代码运行,但我有麻烦的高分代码。我无法使用bufferedreader和printwriter函数,因为我不理解某些原因,它们没有运行。我希望程序将分数与highscore进行比较,如果分数大于highscore,highscore将在txt文件中更新。txt文件之所以是必需的,是因为一旦程序关闭,我需要一种方法来检查以前的高分。我对使用程序处理、写入和读取txt文件非常陌生,我所看到的其他网站和论坛都没有帮助,因为它们没有将highscore变量写入txt文件。请帮帮我,我准
班级高分{
int高分=0;
PrintWriter输出;//导入写入程序
BufferedReader读取器;//导入读取器
int state=0;//设置键盘的变量
无效记分(){
int分数=EM1.0分;
如果(分数>高分){
高分=分数;
}
文本大小(30);
文本(“您的分数为”+分数,150,身高/4);
文本(“当前高分为“+高分+”,75,高度/2);
文本(“试着打败它。”,200450);
文本大小(12);
文本(“按esc键退出此页。”,225,550);
}
无效读取器(){
重要性();
更新EHS();
}
void updateehs(){
int分数=EM1.0分;
output=createWriter(“highscore.txt”);//创建将
如果(高分<分数){
高分=分数;
输出。打印(高分);
output.close();
}
}
无效输入(){
reader=createReader(“highscore.txt”);//读取当前的highscore
如果(读卡器==null){
高分=0;
返回;
}
弦线;
试一试{
line=reader.readLine();
}
捕获(IOE异常){
e、 printStackTrace();
行=空;
}
如果(行!=null){
高分=整数(行);
println(高分);
}
试一试{
reader.close();
}
捕获(IOE异常){
e、 printStackTrace();
}
}
void type(){//在键盘上按esc键时,允许关闭屏幕
状态=键;
如果(键==ESC){
退出();
}
}
}
你就快到了。我只看到了几件稍微有点不对劲的事情:
updateEHS()
中检查if(highscore
会阻止写入:您实际上并不需要这样做,因为scoring()
已经检查当前分数是否为highscore并相应地更新EM1Class EM1 = new EM1Class();
High_Score hs = new High_Score();
void setup(){
size(800,600);
EM1.score = 500;
hs.reader();
}
void draw(){
background(0);
hs.scoring();
hs.updateHS();
}
void keyPressed(){
if(keyCode == UP) EM1.score += 10;
if(keyCode == DOWN) EM1.score -= 10;
}
class EM1Class{
int 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
output.print(highscore);
output.flush();
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();
}
}
}
那里有一个占位符EM1Class
,这样就可以编译草图了
我不确定高分
班级是否应该直接了解EM1
。
您可能需要对代码进行一些重构,并确保它不像当前那样紧密耦合,因为您仍然在使用类
另外请看a:这将有助于编写一致且组织整齐的代码。从长远来看,建立这样做的规程肯定会有回报,但在编写代码时,立即扫描/快速读取和推断代码流也会更容易
另一个选项是使用内置的处理和函数,这将使解析更容易(还具有方便的加载/保存函数)
int score = 0;
int highScore;
void setup(){
loadHighScore();
}
void draw(){
background(0);
text("score:"+score+"\nhighScore:"+highScore+"\n\nuse UP/DOWN\narrows",10,15);
}
void keyPressed(){
if(keyCode == UP) score += 10;
if(keyCode == DOWN) score -= 10;
if(score > highScore) highScore = score;
}
void loadHighScore(){
try{
JSONObject jsonScore = loadJSONObject("highScore.json");
highScore = jsonScore.getInt("highScore");
}catch(Exception e){
println("error loading/parsing highScore.json");
e.printStackTrace();
}
}
void saveHighScore(){
JSONObject jsonScore = new JSONObject();
jsonScore.setInt("highScore",highScore);
saveJSONObject(jsonScore,"highScore.json");
}
//save on close
void exit(){
saveHighScore();
super.exit();
}