gpt4 book ai didi

java - 从文件加载到数组列表时出现问题

转载 作者:行者123 更新时间:2023-12-01 10:09:24 25 4
gpt4 key购买 nike

所以我正在编写一个测验程序,其中包含不确定的问题数量(用 Java),并且我在完成以下事情时遇到问题:

1) 从文件加载测验问题并存储在数组列表中并访问它(需要帮助!)2)正确答案不被接受 - 给我错误(逻辑错误)3) 并未显示所有答案选项

代码如下:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Quiz {

public void writeFile() {
Question qn = new Question();
try {
PrintWriter out = new PrintWriter("quiz.txt");
out.println(qn.Question);
out.println(qn.numberOfChoices);
qn.answerChoices = new String[qn.numberOfChoices];
for (int i = 0; i < qn.numberOfChoices; i++) {
out.println(qn.answerChoices[i]);
}
out.println(qn.correctAnswer);
out.println(qn.numOfTries);
out.println(qn.numOfCorrectTries);
out.close();
} catch (IOException f) {
System.out.println("Error.");
}
qn.getQuestion();
}

public void readFile() {
File file = new File ("quiz.txt");
boolean exists = file.exists();
Quiz q = new Quiz();
Question a = new Question();
List<String> question = new ArrayList<String>();
String[] answerChoices = a.answerChoices;
try {
if (exists == true) {
Scanner s = new Scanner(file);
a.Question = s.nextLine();
a.numberOfChoices = s.nextInt();
a.answerChoices = new String[a.numberOfChoices];
for (int i = 0; i < a.numberOfChoices; i++) {
a.answerChoices[i] = s.nextLine();
}
s.nextLine();
a.correctAnswer = s.nextInt();
a.numOfTries = s.nextInt();
a.numOfCorrectTries = s.nextInt();
a.getQuestion();
} else {
q.writeFile();
}
} catch (IOException e) {
System.out.println("File not found.");
}
}

public static void main (String[] args) {
Scanner in = new Scanner(System.in);
Quiz qz = new Quiz();
Question b = new Question();
int Selection;
List<String> question = new ArrayList<String>();

System.out.println("Welcome to the Quiz Program! Good luck!");
do {
qz.readFile();
System.out.println("Your answer?: ");
Selection = in.nextInt();
if (Selection == b.correctAnswer) {
b.numOfCorrectTries++;
b.getQuestion();
} else {
b.getQuestion();
}
} while (Selection < b.numberOfChoices);
while (Selection > b.numberOfChoices || Selection < 0) {
System.out.println("Error. Try again");
System.out.println("Your answer?: ");
Selection = in.nextInt();
}
}
}

和问题类别:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Question {

int correctAnswer;
int numOfTries;
int numOfCorrectTries;
int numberOfChoices;
String Question;
String[] answerChoices;

public Question() {

}
public void getQuestion() {
System.out.println("Question: " + Question);
System.out.println("Answer: ");
for (int i = 0; i < numberOfChoices; i++) {
System.out.println(answerChoices[i]);
}
}

public double getPercentageRight() {
double percentageRight = (numOfCorrectTries / numOfTries) * 100;
percentageRight = Math.round(percentageRight * 100);
percentageRight = percentageRight / 100;
return percentageRight;
}
}

测验.TXT:

How many licks does it take to get to the tootsie roll center of a  
tootsie pop?
4
one
two
three
four
2
14
5
What is your name?
3
Arthur, King of the Britons
Sir Lancelot the Brave
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot
0
14
6
Who's on first?
5
What
Why
Because
Who
I don't know
3
14
7
Which of the following is a terror of the fire swamp?
4
Lightning sand
Flame spurt
R.O.U.S.
All of the above
3
14
4
Who is the all-time greatest pilot?
6
Manfred von Richthofen
Chuck Yeager
Hiraku Sulu
Luke Skywalker
Kara Thrace
Charles Lindbergh
4
14
9

最佳答案

一些问题:

您的List<String> question = new ArrayList<String>();应该是类似 List<Question> questionBank = new ArrayList<Question>();因为将所有内容都保存为字符串(而不是 Question 对象)会更加困惑。名称questionBank也比 question 更具描述性阅读代码时。我还建议使用 questionBank作为类变量,因此可以在您的 Quiz 中轻松访问它类(class)。

您从未将问题添加到 ArrayList 中,但我怀疑您已经知道这一点,并且在解决其他问题时它的优先级较低。

您的Question类(class)也有点不合常规。更好的结构方式可能是这样的:

public class Question {

private int correctAnswer;
private int numOfTries;
private int numOfCorrectTries;
private String question;
private String[] answerChoices;

public Question(String question, String[] answerChoices,
int correctAnswer, int numOfTries, int numOfCorrectTries) {
this.question = question;
this.answerChoices = answerChoices;
this.correctAnswer = correctAnswer;
this.numOfTries = numOfTries;
this.numOfCorrectTries = numOfCorrectTries;
}

public void getQuestion() {
System.out.println("Question: " + question);
System.out.println("Answer: ");
for (int i = 0; i < answerChoices.length; i++) {
System.out.println(answerChoices[i]);
}
}

public double getPercentageRight() {
double percentageRight = (numOfCorrectTries / numOfTries) * 100;
percentageRight = Math.round(percentageRight * 100);
percentageRight = percentageRight / 100;
return percentageRight;
}

}

我删除了 numberOfChoices 的变量因为这与 answerChoices.length 相同。我还重命名了你的Questionquestion因为Java中的变量通常遵循驼峰命名法。我不确定其他方法的用途或它们应如何显示输出,所以我大多不理会它们。

为了读取文件,我认为您可以执行与您所拥有的类似的操作,但我将发布符合 Question 的新构造函数的代码。类。

private void addQuestions() {
File quizText = new File("quiz.txt");
try {
Scanner fileIn = new Scanner(quizText);
while (fileIn.hasNextLine()) {
String question = fileIn.nextLine();
int numberOfAnswers = fileIn.nextInt();
fileIn.nextLine();
String[] answers = new String[numberOfAnswers];
for (int i = 0; i < numberOfAnswers; i++) {
answers[i] = fileIn.nextLine();
}
int correctAnswer = fileIn.nextInt();
int numOfTries = fileIn.nextInt();
int numOfCorrectTries = fileIn.nextInt();
fileIn.nextLine();
Question nextQuestion =
new Question(question, answers, correctAnswer, numOfTries, numOfCorrectTries);
questionBank.add(nextQuestion);
}
fileIn.close();
} catch (IOException e){
e.printStackTrace();
System.out.println("File Not Found.");
return;
}
}

我还将变量设为私有(private),但您可以为它们创建自定义 getter,以防止从外部直接访问(和/或更改)它们。使用此代码,我能够创建一个包含所有五个问题的问题库,并显示正确的答案以及所有可能的选择,因此希望它能为您指明正确的方向。

关于java - 从文件加载到数组列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232097/

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