将游戏分数保存到文件并确定“高分”

问题描述:

我已经编写了一个随机选择Deutch中的动词并询问Perfekt表单的程序。它不断显示新的动词,直到你犯了一个错误。然后程序会告诉你你的答案是不正确的,并告诉你你有多少分。该程序还将点数写入test.txt文件,然后重新读取该数字。将游戏分数保存到文件并确定“高分”

这里是我的程序代码(它如上所述正确工作)。

import java.util.Random;  
import java.io.File; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Scanner; 
import java.io.*; 

public class Verben{ 
    public static void main(String args[]){ 
    String glagol; 
    String correct = "Correct!"; 
    String incorrect = "Incorrect!"; 
    int points = 0; 
    boolean answer; 

    File file = new File("test.txt"); 


    for(int i = 0; i <= points; i++){ 
     Random random = new Random(); 


     String verben[] = {"trinken", "lesen", "schwimmen", "sterben", "fahren"}; 
     String verbenAnswer[] = {"hat getrunken", "hat gelesen", 
       "hat geschwommen", "ist  gestorben", "ist gefahren"}; 
     glagol = verben[random.nextInt(verben.length)]; 
     System.out.println("Please enter correct form of verb!"); 
     System.out.println(glagol); 
     String enter = Input.readLine(); 

     if(glagol.equals(verben[0]) && enter.equals(verbenAnswer[0])){ 
      answer = true; 
      points += 1; 
     }else if(glagol.equals(verben[1]) && enter.equals(verbenAnswer[1])){ 
      answer = true; 
      points += 1; 
     }else if(glagol.equals(verben[2]) && enter.equals(verbenAnswer[2])){ 
      answer = true; 
      points += 1; 
     }else if(glagol.equals(verben[3]) && enter.equals(verbenAnswer[3])){ 
      answer = true; 
      points += 1; 
     }else if(glagol.equals(verben[4]) && enter.equals(verbenAnswer[4])){ 
      answer = true; 
      points += 1; 
     }else{ 
      answer = false; 
      points += 0; 
     } 

     if(answer == true){ 
      System.out.println(correct); 
     }else{ 
      System.out.println(incorrect); 
      System.out.println("You collected: " + points + "/" + (i+1)); 
     } 
    } 

     try{ 
      PrintWriter output = new PrintWriter(file); 
      output.println(points); 
      output.close(); 
     }catch (FileNotFoundException ex){ 
      System.out.printf("ERROR: %s\n", ex); 
     } 
     try{ 
      Scanner input = new Scanner(file); 
      int point = input.nextInt(); 
      System.out.printf("Points: %d\n", point); 
     }catch(IOException ex){ 
      System.err.println("ERROR"); 
     } 

    } 
} 

我该如何修改我的代码,将所有尝试永久存储在test.txt文件中?我如何确定最高分?每次运行该程序后,我都希望它能提醒我“什么是最高分”。

+1

您的问题不清楚。有什么工作不好?以何种方式 ? – Dici 2014-10-18 20:14:37

+0

没有什么不对,但我想补充一下这个程序,它允许我存储“测试”的结果,然后向我显示所有时间的最高分数。 – mrzlaroka 2014-10-18 20:51:21

一种方法是...

紧接着你的for循环,通过读取你的文件中的行由行确定高分。为了简单起见,我们将假设每行有一个分数。

// determine the high score 
    int highScore = 0; 
    try { 
     BufferedReader reader = new BufferedReader(new FileReader(file)); 
     String line = reader.readLine(); 
     while (line != null)     // read the score file line by line 
     { 
      try { 
       int score = Integer.parseInt(line.trim()); // parse each line as an int 
       if (score > 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"); 
    } 

确定高分后,我们可以显示相应的消息。

// display the high score 
    if (points > highScore) 
    {  
     System.out.println("You now have the new high score! The previous high score was " + highScore); 
    } else if (points == highScore) { 
     System.out.println("You tied the high score!"); 
    } else { 
     System.out.println("The all time high score was " + highScore); 
    } 

最后我们将当前分数附加到文件末尾(在它自己的行上)。

// append the last score to the end of the file 
    try { 
     BufferedWriter output = new BufferedWriter(new FileWriter(file, true)); 
     output.newLine(); 
     output.append("" + points); 
     output.close(); 

    } catch (IOException ex1) { 
     System.out.printf("ERROR writing score to file: %s\n", ex1); 
    } 
} 

如果您想要在程序开始时显示最高分,那么您需要在程序开始时读取该文件。将文本文件中的所有条目添加到ArrayList(或类似的东西)中,然后找到列表中最大的元素,并显示它。 (或者在阅读文件时跟踪最重要的元素,如果以后不需要其他分数的话)。

另外,当您为文件写入分数时,您需要确保将其添加到文件的末尾,而不是覆盖文件。要做到这一点

+0

我会建议一个'LinkedList'来代替,它会更快地追加大量的元素。 – Dici 2014-10-18 20:53:04

+0

我仍然是java编程的初学者,所以你可以写下如何做到这一点,你建议? – mrzlaroka 2014-10-18 21:09:42