2013-11-20 21 views
0

我想從外部文件讀取。我已經成功完成了從文件讀取,但現在我有一個小問題。該文件包含大約88個動詞。動詞都寫在文件中是這樣的:be was been beat beat beaten become became become等等...數組/串的建議,隨機

我現在需要的是,我要像程序的測驗,其中只有兩個由動詞隨機串上來了,並且用戶有填寫缺失的那個。而不是一個缺少的,我想這(「------」)。我的英文不是很好。所以我希望你明白我的意思。

System.out.println("Welcome to the programe which will test you in english verbs!"); 
System.out.println("You can choose to be tested in up to 88."); 
System.out.println("In the end of the programe you will get a percentage of total right answers."); 

Scanner in = new Scanner(System.in); 
System.out.println("Do you want to try??yes/no"); 

String a = in.nextLine(); 
if (a.equals("yes")) { 
    System.out.println("Please enter the name of the file you want to choose: "); 

} else { 
    System.out.println("Programe is ended!"); 
} 



String b = in.nextLine(); 
while(!b.equals("verb.txt")){ 
    System.out.println("You entered wrong name, please try again!"); 
    b = in.nextLine(); 

} 
System.out.println("How many verbs do you want to be tested in?: "); 
int totalVerb = in.nextInt(); 
in.nextLine(); 


String filename = "verb.txt"; 
File textFile = new File(filename); 
Scanner input = new Scanner(textFile); 

for (int i = 1; i <= totalVerb; i++){ 



    String line = input.nextLine(); 

    System.out.println(line); 
    System.out.println("Please fill inn the missing verb: "); 
    in.next(); 
} 

System.out.println("Please enter your name: "); 
in.next(); 
+0

有:「兩個stringsfrom動詞」你的意思是動詞的兩個不同的時態,用戶必須輸入第三緊張,對不對? – kai

+0

右啓。隨機地,其中一個將會有「-----」。 – user3012649

回答

0

我不太明白「問題」是什麼。我假設你可以正確地閱讀文件,所以我不會評論該代碼。關於如何顯示動詞和用戶填寫正確的問候,你可以像這樣的東西去:

  1. 閱讀這三個動詞
  2. 將三個動詞在String []數組或在一個ArrayList
  3. 打印陣列[0] + 「----」 +陣列[2]
  4. 迴路的輸入
  5. 比較,如果輸入是等於陣列[1]
  6. 如果是等號從循環中突破並進入下三個動詞

希望這個僞代碼有幫助。

更新

在工序我指的是以下幾點:

String[] conjugations = new String[3]; 
//You have added the conjugations to this array with conjugations[position]=conjugation; 
System.out.println(conjugations[0] + " ----- " + conjugations[2]); 

假設的過程如下:

  • 您已創建的String []動詞變化
  • 你已經閱讀了三個conjuga從文件
  • 動詞的蒸發散你已經擺在每一個結合您的綴合陣列

裏面是什麼也承擔?

  • 這一切正在測試的動詞始終只有3個綴合,你總是在相同的時態結合測試(即雲在陣列的1位的一個。)

如果你想它是完全隨機的(三種變形中的任何一種),那麼你需要使用java.util.Random類,如下所示:

下面假定你已經引入了動詞「擊敗「到你的變形字符串數組(String []變形)。

Random r = new Random();  
int posOfConjugationToGuess = r.nextInt(conjugations.length);  
String phrase = "";  
String conjugationToGuess; 

for(int i=0; i<conjugations.length;++i){ 
    if(i==posOfConjugationToGuess){ 
     conjugationToGuess=conjugations[i]; 
     phrase+=" ----- ";  
    }  
    else{ 
     phrase+=" " + conjugations[i] + " "; 
    } 
} 
System.out.println(phrase) 

;

而當用戶輸入內容時,您會再次檢查它conjugationToGuess。我現在將解釋一些代碼。

Random r = new Random(); 
int posOfConjugationToGuess = r.nextInt(conjugations.length); 

這段代碼可以讓你得到介於0(含),無論您通過它的構造(獨家)的隨機數。在這種情況下,它將是3,因爲這是你的變形數組的長度。

爲什麼我放這個?那麼如果你想說的話,讓我們說4,那麼在某一點你會得到隨機整數3,當你試圖做conjugations[3]時,你會得到一個IndexOutOfBoundsError

for循環只是用來構造短語來打印。怎麼樣?這個想法是,你會遍歷你的變化數組,添加每個單詞的短語,但如果你的隨機位置(當i==posOfConjugationToGuess)決定了你所要求的共軛,你不會把它添加到短語,而是放在「------」中。就這樣。

再一次,這裏假設你所有的動詞只有三個變形。如果共軛不總是三個,那麼你需要使用一個ArrayList。希望這可以幫助。

+0

「打印數組[0] +」----「+數組[2]」是什麼意思?怎麼做? – user3012649

+0

你知道你是如何在System.out.println(...)的屏幕上打印的嗎?那麼,同樣的方式。我會更新我的答案。 – Chayemor

0

一個簡單的方法來做到這一點會實現它是這樣的:

首先,讀取文本文件三個動詞到字符串數組大小3. 的[],然後使用了java.util.Random使用nextInt(int N)方法從0-2生成一個int i。 例如,如果int i被隨機選爲1,那麼如果/ else結構顯示數組[0]和數組[2]的內容,則可以使用開關或類似的 ,然後要求輸入用戶輸入缺失的動詞時態。比較用戶輸入和非顯示數組的內容(在本例中是array [1]),並返回它是否匹配。

您還應該將用戶輸入標準化爲全部小寫(使用.toLowerCase()),以便用戶不會因爲大小寫而使答案錯誤。

0

首先,你需要從文件中讀取的所有數據:

String[][] verbs = new String[30][3]; //create a new 2D array 
Scnanner inFile = new Scanner(new File("<file name here>.txt")); //create a new Scanner object to read data in from a file 
for(int i = 0; inFile.hasNext(); i++) //while inFile still has data that hasn't been read; increment i 
{ 
    for(int j = 0; j < 3; j++) //loop 3 times 
    { 
     verbs[i][j] = inFile.next(); //set read data and put it in "verbs" 
    } 
} 

在此之後,你將不得不從文件中2D array包含所有的動詞。二維數組中的每一行都包含一個三個動詞相同但時間不同的數組。

現在,您需要選擇和顯示兩個隨機動詞:

Random random = new Random(); //create a new Random object, which we can use to generate random numbers 
int row = random.nextInt(verbs.length) //pick a random set of 3 verbs 
columnExcluded = random.nextInt(3) //pick a random verb to guess 

System.out.print("Here are two verbs: "); 

for(int i = 0; i < 3; i++) //loop 3 times 
{ 
    if(i != columnExcluded) //if this verb is not the one that the person should guess 
    { 
     System.out.print(verbs[row][i] + ", "); //println the verb 
    } 
} 
System.out.println("_____"); //print blank line 
System.out.print("What is the third tense of this verb? "); 
String verbGuess = in.nextLine(); //take the guess 
if(verbGuess.equalsIgnoreCase(verbs[row][columnExcluded]) //if verb guessed equals the missing verb 
{ 
    System.out.println("You are right!"); 
} 
else 
{ 
    System.out.println("You are wrong. D:"); 
}