Java 使用文件中的字符串的建议
我正在尝试读取外部文件。我已成功地从文件中读取,但现在我有一个小问题。该文件包含大约88个动词。动词在文件中的书写方式如下: 曾经 跳动 变成 等等Java 使用文件中的字符串的建议,java,string,random,Java,String,Random,我正在尝试读取外部文件。我已成功地从文件中读取,但现在我有一个小问题。该文件包含大约88个动词。动词在文件中的书写方式如下: 曾经 跳动 变成 等等 我现在需要帮助的是,我想要一个类似测验的程序,其中只有两个来自动词的随机字符串出现,用户必须填充缺少的字符串。我要的是这个,而不是丢失的那个。我的英语不是很好,所以我希望你明白我的意思 System.out.println("Welcome to the programe which will test you in english ver
我现在需要帮助的是,我想要一个类似测验的程序,其中只有两个来自动词的随机字符串出现,用户必须填充缺少的字符串。我要的是这个,而不是丢失的那个。我的英语不是很好,所以我希望你明白我的意思
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");
}
}
以上是完整的运行程序。更具体地说,您不能做什么?我们不想运行您的代码来找出它。是否要检查文件中是否存在谓词或其他内容?我现在需要的帮助是,我需要一个类似于测验的程序,其中只会出现谓词中的两个随机字符串,用户必须填充缺少的字符串。不是缺少一个,我想要这个---。是这样的吗,每行有3个动词present,pass,participle,你想显示这三个词中的任意两个,用户必须填写第三个。你说的“动词字符串”是什么意思?我真的不明白。你可以用它来代替for循环。while循环逐行遍历整个文件。当没有更多的行时,循环结束。我还添加了一些代码来检查答案是否正确并保持计数。谢谢你的帮助,我的朋友,但是你的示例太高级了。我是一个新手,不能理解你写的很多东西。任何简单的方法?复制粘贴我的代码并运行它。这是一个完整的运行程序。你不必对它做任何事情,只要运行它。请确保您输入了正确的文件名或程序,因为它会给您一个错误。我在换行时遇到了错误,情况是0、1和2。为什么?错误在于无法将换行符解析为变量。