簡體   English   中英

帶數組/字符串的建議,隨機

[英]Suggestions with array/string, random

我正在嘗試從外部文件讀取。 我已經成功完成了從文件中的讀取,但是現在我遇到了一些問題。 該文件包含大約88個動詞。 這些動詞是這樣寫在文件中的: be was been beat beat beaten become became become ...等等。

現在我需要的是一個類似測驗的程序,其中動詞中只有兩個隨機字符串會出現,並且用戶必須填寫一個缺少的字符串。 我需要this(“ ------”)而不是一個。 我的英語不怎么好。 所以我希望你明白我的意思。

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

我不太了解“問題”是什么。 我認為您可以正確讀取文件,因此我不會在該代碼上發表評論。 關於如何顯示動詞以及為用戶填寫正確的動詞,您可以使用類似以下內容的方法:

  1. 閱讀三個動詞
  2. 將三個動詞放在String []數組或ArrayList中
  3. 打印數組[0] +“ ----” +數組[2]
  4. 輸入循環
  5. 比較輸入是否等於array [1]
  6. 如果等於,則從循環中斷開並轉到接下來的三個動詞

希望此偽代碼有幫助。

更新資料

在步驟3中,我的意思是:

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 []共軛
  • 您已經從文件中閱讀了一個動詞的三個詞形變化
  • 您已將每個共軛放置在共軛數組中

這還假設什么?

  • 您要測試的所有動詞始終僅具有3個共軛,並且您始終在測試相同的時態共軛(在數組的位置1處)。

如果您希望它是完全隨機的(三個共軛),則需要使用java.util.Random類,如下所示:

以下假設您已將動詞“ beat”的三個詞綴引入到詞綴String數組(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。 希望這可以幫助。

一個簡單的方法是像這樣實現它:

首先,將三個動詞從文本文件中讀取為大小為3的String Array []。然后使用Java.util.Random使用nextInt(int N)方法從0-2生成一個int i。 例如,如果將int i隨機選擇為1,則可以使用開關或類似的if / else結構來顯示array [0]和array [2]的內容,然后要求輸入用戶輸入缺少的內容動詞時態。 將用戶輸入的內容與未顯示的數組(在本例中為array [1])的內容進行比較,並返回是否匹配。

您還應該將用戶輸入全部小寫(使用.toLowerCase())來規范化您的用戶輸入,以免用戶因大小寫而得到錯誤的答案。

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

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數組,其中包含文件中的所有動詞。 2D數組中的每一行都包含一個帶有三個相同但時態不同的動詞的數組。

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

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:");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM