首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数组/字符串的建议,随机的

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

Stack Overflow用户
提问于 2013-11-20 12:25:42
回答 3查看 127关注 0票数 0

我正在尝试从外部文件中读取。我已经成功地完成了从文件中读取,但现在我有一个小问题。该文件包含大约88个动词。动词在文件中写成这样:be was been beat beat beaten become became become等等.

我现在需要的是,我想要一个类似于程序的小测验,只有两个来自动词的随机字符串出现,用户必须填写一个缺失的字符串。,我想要这个(“-”)。我的英语不太好。所以我希望你能理解我的意思。

代码语言:javascript
运行
复制
    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();
EN

回答 3

Stack Overflow用户

发布于 2013-11-20 12:34:01

我不太明白“问题”是什么。我假设您可以正确地读取该文件,因此我不会对该代码进行注释。关于如何显示动词并让用户填写正确的动词,您可以使用以下内容:

  1. 读这三个动词
  2. 将这三个动词放置在String[]数组或ArrayList中
  3. 打印数组+“--”+ array2
  4. 输入回路
  5. 比较输入是否等于array1
  6. 如果它等于,请从循环中断开,转到下面的三个动词上

希望这个伪码能帮上忙。

更新

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

代码语言:javascript
运行
复制
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[]共轭
  • 你已经从文件中读到了动词的三个连词
  • 你在你的共轭阵列中放置了所有的共轭

这也假设了什么?

  • 所有被测试的动词总是只有三个共轭,而你总是测试同一个时态共轭(数组的位置1中的那个)。

如果您希望它是完全随机的(三种共轭中的任一种),那么您需要使用java.util.Random类,如下所示:

下面假设您已经在共轭字符串数组(String[]共轭)中引入了动词“拍子”的三个共轭。

代码语言:javascript
运行
复制
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。我现在来解释一下代码。

代码语言:javascript
运行
复制
 Random r = new Random();
int posOfConjugationToGuess = r.nextInt(conjugations.length);

此代码允许您在0(包括)和通过构造函数(exclusive).传递的任意值之间获得一个随机数。在这种情况下,它将是3,因为这是你的共轭数组的长度。

我为什么要放这个?如果你要放4,那么在某个时候你会得到随机整数3,当你尝试做conjugations[3]时,你会得到一个IndexOutOfBoundsError

for循环只是用来构造要打印的短语。多么?这个想法是,你会迭代你的共轭数组,把每个单词加到短语中,但是如果你遇到随机位置( i==posOfConjugationToGuess),它决定了你想要的共轭,你没有把它添加到短语中,而是插入“-”。就这样。

再一次,这假设所有的动词都只有三个共轭。如果共轭不一定是3,那么您将需要使用ArrayList。希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2013-11-20 13:11:04

要做到这一点,一个简单的方法是这样实现:

首先,将文本文件中的三个动词读入大小为3的字符串Array[],然后使用Java.util.Random使用nextInt( into )方法从0-2生成int I。例如,如果int被随机选择为1,您将使用一个开关或类似的if/else结构来显示数组和array2的内容,然后请求输入用户输入的缺失动词时态。将用户输入与未显示数组的内容进行比较(在本例中为array1),并返回是否匹配。

您可能还应该将用户输入标准化,将其设置为所有小写(使用.toLowerCase()),这样用户就不会因为大小写而出错。

票数 0
EN

Stack Overflow用户

发布于 2013-11-20 13:47:16

首先,您需要从文件中读取所有数据:

代码语言:javascript
运行
复制
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数组中的每一行都包含一个包含三个动词的数组,它们是相同的,但时态不同。

现在您需要选择并显示两个随机动词:

代码语言:javascript
运行
复制
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:");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20095958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档