gpt4 book ai didi

java - 如何从特定结构的txt文件中获取试题?

转载 作者:行者123 更新时间:2023-12-02 06:20:31 24 4
gpt4 key购买 nike

我想制作一个程序,从具有特定结构的文件中获取问题及其答案,并让用户给出答案。该程序还必须计算正确答案并将其显示给用户。

这是文本文件结构的示例:

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

}

我将非常感谢您提供的任何帮助。如果有更好的方法来完成任务,我将很高兴看到。

提前致谢!

最佳答案

这是您的程序的更正版本。你那里有一些错误。我的程序在您发布的文件格式上运行正常在这里(没有星号)。

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:/Various/test.txt"));
StringBuilder sb = new StringBuilder();
String line = null;

do {
line = br.readLine();
if (line != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
} else {
break;
}
String answer = br.readLine().trim();
while (!"---".equals(line)) {
line = br.readLine();
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
System.out.println(sb.toString());
answerCheck(answer);
sb.setLength(0);

} while (true);

} 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.equals(rightAnswer)) {
answerCounter++;
System.out.println("Correct!");
} else {
System.out.println("Wrong!");
}
}

}

关于java - 如何从特定结构的txt文件中获取试题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com