我已經編寫了一個隨機選擇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文件中?我如何確定最高分?每次運行該程序後,我都希望它能提醒我「什麼是最高分」。
您的問題不清楚。有什麼工作不好?以何種方式 ? – Dici 2014-10-18 20:14:37
沒有什麼不對,但我想補充一下這個程序,它允許我存儲「測試」的結果,然後向我顯示所有時間的最高分數。 – mrzlaroka 2014-10-18 20:51:21