我编写了一个程序,在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文件中的所有尝试?我怎样才能确定历史最高分?每次运行程序后,我都想提醒我“什么是历史最高分”。
答案 0 :(得分:1)
这样做的一种方法是......
在你的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);
}
}
答案 1 :(得分:0)
如果要在程序开头显示历史最高分,则需要在程序开头读取文件。将文本文件中的所有条目添加到ArrayList(或类似的东西)中,然后找到列表中最大的元素,显示它。 (或者,如果您以后不需要其他分数,请在阅读文件时跟踪最大元素。)
此外,当您为文件写分数时,您需要确保将其添加到文件的末尾,而不是覆盖文件。