【问题标题】:Suggestions with Strings from File文件中字符串的建议
【发布时间】:2013-12-04 07:23:18
【问题描述】:

我正在尝试从外部文件中读取。我已经成功地从文件中读取,但现在我有一个小问题。该文件包含大约 88 个动词。动词是这样写在文件中的: 曾经是 打打打打 成为 成为 成为 等等……

我现在需要帮助的是我想要一个类似程序的测验,其中只会出现两个来自动词的随机字符串,并且用户必须填写缺少的那个。我想要这个(“------”),而不是丢失的那个。我的英语不太好,所以我希望你明白我的意思。

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

【问题讨论】:

  • 更具体一些,你不能做什么?我们不想运行您的代码来弄清楚。
  • 您要检查文件中是否存在动词或其他东西吗?
  • 我现在需要帮助的是我想要一个类似程序的测验,其中只会出现来自动词的两个随机字符串,并且用户必须填写缺少的那个。我想要这个(“------”),而不是丢失的那个。
  • 是这样吗,每行有3个动词(现在,过去,分词),你想显示这三个中的任何两个,用户必须填写第三个。???跨度>
  • “动词中的字符串”是什么意思?我不太明白。

标签: java string random


【解决方案1】:

你可以这样做

import java.util.Scanner;
import java.io.*;

public class GuessVerb {

    public static void main(String[] args) throws IOException{

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a file name: ");

        String fileName = in.nextLine();
        File file = new File(fileName);

        Scanner input = new Scanner(file);

        String guess = null;
        int correctCount = 0;

        while(input.hasNextLine()) {
            String line = input.nextLine();          // get the line

            String[] tokens = line.split("\\s+");    // split it into 3 word

            int randNum = (int)(Math.random() * 3);  // get a random number 0, 1, 2

            String newLine = null;                          // new line

            int wordIndex = 0;
            switch(randNum){                         // case for random number
                case 0: newLine = "------ " + tokens[1] + " " + tokens[2]; 
                            wordIndex = 0; break;
                case 1: newLine = tokens[0] + " ------ " + tokens[2]; 
                            wordIndex = 1; break;
                case 2: newLine = tokens[0] + " " + tokens[1] + " -------";
                            wordIndex = 2; break;
           }

           System.out.println(newLine);

           System.out.println("Please fill inn the missing verb: ");
           guess = in.nextLine();

           if (guess.equals(tokens[wordIndex])){
               correctCount++;
           }
        }
        System.out.println("You got " + correctCount + " right");
    }
}

以上是完整的运行程序。

【讨论】:

  • 您也许可以用它替换您的 for 循环。 while 循环逐行遍历整个文件。当没有更多行时,循环结束。
  • 我还添加了一些代码来检查答案是否正确并保持计数。
  • 感谢朋友的帮助,但是你的例子太高级了。我是新手,看不懂你写的很多东西。有什么简单的方法吗?
  • 复制并粘贴我的代码并运行它。这是一个完整的运行程序。您无需对其执行任何操作,只需运行它即可。确保你输入了正确的文件名或程序给你一个错误。
  • 在案例 0、1 和 2 中,“newLine”出现错误。为什么?错误是“newLine 无法解析为变量”。
猜你喜欢
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多