繁体   English   中英

来自文件字符串的建议

[英]Suggestions with Strings from File

我正在尝试从外部文件读取。 我已经从文件中成功读取,但是现在我有一个小问题。 该文件包含大约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();

你可以做这样的事情

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

以上是完整的运行程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM