【问题标题】:Having difficulties with making my processing PrintWriter and BufferedReader work使我的处理 PrintWriter 和 BufferedReader 工作有困难
【发布时间】: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


    【解决方案1】:

    你快到了。我只看到了几件事略有不同:

    1. 您没有在编写器上调用 flush() 将剩余数据写入文件
    2. 在updateHS() 中检查if (highscore &lt; score) 会阻止写入:无论如何,您实际上并不需要这个,因为scoring() 已经检查当前分数是否为高分并相应更新。

    以下是调整为从磁盘写入/读取的大部分代码:

    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,以便可以编译草图。

    我不确定High_Score 类是否应该直接了解EM1。 您可能需要稍微重构代码并确保它不像当前那样紧密耦合,因为您仍然使用类。

    还可以查看Java style guide:它将有助于编写一致且组织整齐的代码。从长远来看,建立纪律肯定会带来回报,而且当你的代码更容易扫描/快速阅读并推断出它的流程时,它也会立即得到回报。

    另一个选项是使用 Processing 内置的 XML 和 JSON 函数,这将更容易解析(并且还有方便的加载/保存函数)。

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2018-06-26
      • 2020-11-20
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多