我正在尝试从外部文件中读取。我已经成功地完成了从文件中读取,但现在我有一个小问题。该文件包含大约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();发布于 2013-11-20 12:34:01
我不太明白“问题”是什么。我假设您可以正确地读取该文件,因此我不会对该代码进行注释。关于如何显示动词并让用户填写正确的动词,您可以使用以下内容:
希望这个伪码能帮上忙。
更新
在步骤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]);当然,假设如下:
这也假设了什么?
如果您希望它是完全随机的(三种共轭中的任一种),那么您需要使用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(包括)和通过构造函数(exclusive).传递的任意值之间获得一个随机数。在这种情况下,它将是3,因为这是你的共轭数组的长度。
我为什么要放这个?如果你要放4,那么在某个时候你会得到随机整数3,当你尝试做conjugations[3]时,你会得到一个IndexOutOfBoundsError。
for循环只是用来构造要打印的短语。多么?这个想法是,你会迭代你的共轭数组,把每个单词加到短语中,但是如果你遇到随机位置( i==posOfConjugationToGuess),它决定了你想要的共轭,你没有把它添加到短语中,而是插入“-”。就这样。
再一次,这假设所有的动词都只有三个共轭。如果共轭不一定是3,那么您将需要使用ArrayList。希望这能有所帮助。
发布于 2013-11-20 13:11:04
要做到这一点,一个简单的方法是这样实现:
首先,将文本文件中的三个动词读入大小为3的字符串Array[],然后使用Java.util.Random使用nextInt( into )方法从0-2生成int I。例如,如果int被随机选择为1,您将使用一个开关或类似的if/else结构来显示数组和array2的内容,然后请求输入用户输入的缺失动词时态。将用户输入与未显示数组的内容进行比较(在本例中为array1),并返回是否匹配。
您可能还应该将用户输入标准化,将其设置为所有小写(使用.toLowerCase()),这样用户就不会因为大小写而出错。
发布于 2013-11-20 13:47:16
首先,您需要从文件中读取所有数据:
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数组中的每一行都包含一个包含三个动词的数组,它们是相同的,但时态不同。
现在您需要选择并显示两个随机动词:
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:");
}https://stackoverflow.com/questions/20095958
复制相似问题