简体   繁体   English

带数组/字符串的建议,随机

[英]Suggestions with array/string, random

I am trying to read from a external file. 我正在尝试从外部文件读取。 I have successfully done reading from the file, but now I have a little problem. 我已经成功完成了从文件中的读取,但是现在我遇到了一些问题。 The file contains around 88 verbs. 该文件包含大约88个动词。 The verbs are written in the file like this: be was been beat beat beaten become became become and so on... 这些动词是这样写在文件中的: be was been beat beat beaten become became become ...等等。

What I need now is that I want a quiz like program where only two random strings from the verb will come up and the user have to fill in the one which is missing. 现在我需要的是一个类似测验的程序,其中动词中只有两个随机字符串会出现,并且用户必须填写一个缺少的字符串。 Instead of the one missing, I want this("------") . 我需要this(“ ------”)而不是一个。 My english is not so good. 我的英语不怎么好。 So I hope you understand what I mean. 所以我希望你明白我的意思。

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

I don't quite understand what the "problem" is. 我不太了解“问题”是什么。 I assume you can read the file properly, so I won't be commenting on that code. 我认为您可以正确读取文件,因此我不会在该代码上发表评论。 On regards on how to show the verb and for the user to fill in the correct one, you could go with something like this: 关于如何显示动词以及为用户填写正确的动词,您可以使用类似以下内容的方法:

  1. Read the three verbs 阅读三个动词
  2. Place the three verbs in a String[] array or in an ArrayList 将三个动词放在String []数组或ArrayList中
  3. Print array[0] + "----" + array[2] 打印数组[0] +“ ----” +数组[2]
  4. Loop for input 输入循环
  5. compare if input is equals to array[1] 比较输入是否等于array [1]
  6. if it is equals break from the loop and go onto the next three verbs 如果等于,则从循环中断开并转到接下来的三个动词

Hope this pseudocode helps. 希望此伪代码有帮助。

Update 更新资料

On step 3 I mean the following: 在步骤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]);

Assuming of course the following: 当然假设以下内容:

  • You have created your String[] conjugations 您已经创建了String []共轭
  • You have read the three conjugations of a verb from the file 您已经从文件中阅读了一个动词的三个词形变化
  • You have placed every conjugation inside your conjugations array 您已将每个共轭放置在共轭数组中

What does this also assume? 这还假设什么?

  • That all the verbs you are testing always have 3 conjugations only and you are always testing for the same tense conjugation (the one that goes in position 1 of the array.) 您要测试的所有动词始终仅具有3个共轭,并且您始终在测试相同的时态共轭(在数组的位置1处)。

If you wanted it to be complete random (either of the three conjugations) then you'd need to use the java.util.Random class, as follows: 如果您希望它是完全随机的(三个共轭),则需要使用java.util.Random类,如下所示:

The following assumes that you have introduced the three conjugations of the verb "beat" into your conjugations String array (String[] conjugations). 以下假设您已将动词“ 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)

; ;

And when the user inputs something, you'll check it agains conjugationToGuess . 当用户输入内容时,您将再次检查conjugationToGuess I'll explain a bit of the code now. 我现在将解释一些代码。

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

This code allows you to get a random number between 0 (inclusive) and whatever you pass through its constructor ( exclusive ). 此代码允许您获取介于0(含)和通过其构造函数( 独占 )之间的任何值之间的随机数。 In this case it will be 3, because that's the length of your conjugations array. 在这种情况下,它将为3,因为这是共轭数组的长度。

Why did I put this? 我为什么要放这个? Well if you were to put, let's say 4, then at some point you'll get the random integer 3, and when you try to do conjugations[3] you'll get an IndexOutOfBoundsError . 好吧,如果要放进去,比方说4,那么在某个时候您将获得随机整数3,并且当您尝试进行conjugations[3]您将获得IndexOutOfBoundsError

The for loop is simply used to construct the phrase to print. for循环仅用于构造要打印的短语。 How? 怎么样? The idea is that you'll iterate through your conjugations array adding every word to the phrase, but if you come upon the random position (when i==posOfConjugationToGuess ) that determines the conjugation you are asking for, you don't add it to the phrase, but instead put in "------". 这个想法是,您将遍历共轭数组,将每个单词添加到短语中,但是如果您遇到随机位置 (当i==posOfConjugationToGuess )会确定您要的共轭,则无需将其添加到该短语,而是放入“ ------”。 That's all. 就这样。

Once again, this assumes that all your verbs only have three conjugations . 再一次,这假设您所有的动词只有三个词缀 If the conjugations are not always three, then you'll need to use an ArrayList. 如果共轭并不总是三个,那么您将需要使用ArrayList。 Hope this helps. 希望这可以帮助。

An easy way to do this would be to implement it like this: 一个简单的方法是像这样实现它:

Firstly, read the three verbs from the text file into an String Array[] of size 3. Then use Java.util.Random to generate an int i from 0-2 using the nextInt(int N) method. 首先,将三个动词从文本文件中读取为大小为3的String Array []。然后使用Java.util.Random使用nextInt(int N)方法从0-2生成一个int i。 If, for example, int i was randomly chosen to be 1, you would use a switch or similar if/else structure to display the contents of the array[0] and array[2], then ask for input user input for the missing verb tense. 例如,如果将int i随机选择为1,则可以使用开关或类似的if / else结构来显示array [0]和array [2]的内容,然后要求输入用户输入缺少的内容动词时态。 Compare the user input to the contents of the non-displayed array (in this case, array[1]), and return whether it matches. 将用户输入的内容与未显示的数组(在本例中为array [1])的内容进行比较,并返回是否匹配。

You should also probably normalize your user input by putting it to all lowercase (using .toLowerCase()) so that users don't get the answer wrong because of case. 您还应该将用户输入全部小写(使用.toLowerCase())来规范化您的用户输入,以免用户因大小写而得到错误的答案。

First, you need to read all the data from the file: 首先,您需要从文件中读取所有数据:

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"
    }
}

After this, you will have a 2D array containing all the verbs from the file. 之后,您将拥有一个2D数组,其中包含文件中的所有动词。 Each row in the 2D array contains an array with the three verbs that are the same, but different tenses. 2D数组中的每一行都包含一个带有三个相同但时态不同的动词的数组。

You now need to pick and display two random verbs: 现在,您需要选择并显示两个随机动词:

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