Java 如何从具有特定结构的txt文件中获取试题?
我想做一个程序,从特定结构的文件中获取问题和答案,并让用户给出答案。程序还必须计算正确答案并向用户显示 以下是文本文件结构的示例:Java 如何从具有特定结构的txt文件中获取试题?,java,substring,bufferedreader,stringbuilder,filereader,Java,Substring,Bufferedreader,Stringbuilder,Filereader,我想做一个程序,从特定结构的文件中获取问题和答案,并让用户给出答案。程序还必须计算正确答案并向用户显示 以下是文本文件结构的示例: What year is it right now? 2 1) 1900 2) 2014 3) 3200 --- Which is the biggest country in the world? 1 1) Russia 2) United States of America 3) United Kingdom --- 这是我写的代码,但有点不对劲,我看不出到底
What year is it right now?
2
1) 1900
2) 2014
3) 3200
---
Which is the biggest country in the world?
1
1) Russia
2) United States of America
3) United Kingdom
---
这是我写的代码,但有点不对劲,我看不出到底是什么:
public class testLoader {
private static BufferedReader br;
private static int answerCounter;
public static void main(String[] args) {
try {
br = new BufferedReader(new FileReader("D:/test.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String answer=null;
if(line.startsWith("*")){
answer = line;
}
while (line != "---") {
line = br.readLine();
sb.append(line);
sb.append(System.lineSeparator());
}
System.out.println(sb.toString());
answerCheck(answer);
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("You have " + answerCounter + "correct answers");
}
public static void answerCheck(String rightAnswer) {
System.out.println("What's your answer?");
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
answerCounter = 0;
if (answer == rightAnswer){
answerCounter++;
System.out.println("Correct!");
} else {
System.out.println("Wrong!");
}
}
}
我将感激你能给予的任何帮助。如果有更好的方法来完成这项任务,我很乐意看到
提前谢谢 这是您的程序的正确版本。 你那里有一些虫子。 我的程序在您发布的文件格式上运行正常 这里(不带星号)
answer
string变量equals()
方法进行比较,因为您处理的是字符串的值,而不是字符串的内存地址answerCounter
变量不会保存用户的全部正确答案,因为无论何时调用该方法,您总是将其赋值为0import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class testLoader
{
private static BufferedReader br;
private static int answerCounter = 0;
public static void main(String[] args)
{
try
{
br = new BufferedReader(new FileReader("C:/test.txt"));
StringBuilder sb;
String line, answer = null;
while((line = br.readLine()) != null)
{
sb = new StringBuilder();
do
{
if(line.length() == 1) //This is a bad choice of digit comparison but it will work with your case (lol)
{
answer = line;
line = br.readLine();
continue;
}
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
while(!line.equals("---"));
System.out.println(sb.toString());
answerCheck(answer);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
System.out.println("You have " + answerCounter + "correct answers");
}
public static void answerCheck(String rightAnswer)
{
System.out.println("What's your answer?");
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
if(answer.equals(rightAnswer))
{
answerCounter++;
System.out.println("Correct!");
}
else
{
System.out.println("Wrong!");
}
}
}
运行程序时可能会发生重复的情况,以及它与您期望的情况有何不同?Thx我在equals()中看到了这一点,但代码仍然不能像我希望的那样工作。。用equals代替“!=”只显示“你的答案是什么:”并检查答案。但StringBuilder对象总是“错误”的。你应该检查:)在回答第一个问题后,它不会提示第一个问题和第二个问题吗?@mehmetgüngör谢谢你注意到这一点,它现在已经修复了。你能告诉我“sb.append(System.getProperty(“line.separator”);”和“sb.append(System.lineSeparator());”之间的区别吗为什么第一个更好?@Crypto我不认为有什么区别。但是我使用的是JDK 6,我认为这种方法是在JDK 7中引入的。所以我没有这个方法。如果它对你有用,那就没关系。所以如果你在这里检查,你会发现根本没有这样的方法
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class testLoader
{
private static BufferedReader br;
private static int answerCounter = 0;
public static void main(String[] args)
{
try
{
br = new BufferedReader(new FileReader("C:/test.txt"));
StringBuilder sb;
String line, answer = null;
while((line = br.readLine()) != null)
{
sb = new StringBuilder();
do
{
if(line.length() == 1) //This is a bad choice of digit comparison but it will work with your case (lol)
{
answer = line;
line = br.readLine();
continue;
}
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
while(!line.equals("---"));
System.out.println(sb.toString());
answerCheck(answer);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
System.out.println("You have " + answerCounter + "correct answers");
}
public static void answerCheck(String rightAnswer)
{
System.out.println("What's your answer?");
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
if(answer.equals(rightAnswer))
{
answerCounter++;
System.out.println("Correct!");
}
else
{
System.out.println("Wrong!");
}
}
}