Java 程序不会向 ArrayList 添加对象 [英] Java program doesn't add objects to ArrayList
问题描述
我正在尝试向 ArrayList
添加不同的对象,但它不起作用.我不确定是因为我添加的对象不正确还是有其他问题.
I'm trying to add different objects to an ArrayList
and it's not working. I'm not sure if it's because I'm adding objects incorrectly or if there's something else wrong.
这是我的代码.
import java.util.*;
import java.io.*;
public class QuizBowl implements Quiz {
private Player player; // player object
private String file; // name of file
private int qNum; // number of questions player wants to answer
private int qNumFile; // number of questions in file
private ArrayList<Question> questionsArr; // holds Question objects
private boolean questionsAsked[]; // array of questions that were already asked
// Constructor
public QuizBowl(String fName, String lName, String file) throws FileNotFoundException {
player = new Player(fName, lName);
Scanner gameFile = new Scanner(new File(file));
qNum = numOfQuestionsToPlay();
qNumFile = maxNumQFile(gameFile);
questionsArr = new ArrayList<Question>();
readFile();
System.out.println(questionsArr);
questionsAsked = new boolean[qNumFile];
System.out.println(Arrays.toString(questionsAsked));
}
// asks user how many questions to ask
public int numOfQuestionsToPlay() {
Scanner console = new Scanner(System.in);
// CHECKS FOR VALID USER INPUT
boolean check = false;
do {
try {
System.out.print("How many questions would you like (out of 3)? ");
int answer = console.nextInt();
if(answer > 3) {
System.out.println("Sorry, that is too many.");
check = false;
}
else {
check = true;
}
}
catch(InputMismatchException e) {
System.out.println("Invalid input. Please try again.");
console.nextLine();
check = false;
}
}
while(check == false);
return qNum;
}
// figures out how many questions are in the file
public int maxNumQFile(Scanner gameFile) {
int num = gameFile.nextInt();
return num;
}
// LOOP FOR READING QUESTIONS
public void readFile() {
for(int i = 0; i < qNum; i++) {
readQuestion();
}
}
// READS QUESTION AFTER QUESTION AND ADDS TO THE ARRAYLIST OF QUESTIONS OBJECTS
public void readQuestion() {
Scanner console = new Scanner(System.in);
console.nextLine();
String[] line;
String question, questionType;
int points;
line = console.nextLine().split(" ");
questionType = line[0];
points = Integer.parseInt(line[1]);
question = console.nextLine();
if(questionType.equals("MC")) { // checks if question type is MC
questionsArr.add(readMC(questionType, question, points, console)); // adds MC question to array
}
else if(questionType.equals("SA")) { // checks if question type is SA
questionsArr.add(readSA(questionType, question, points, console)); // adds SA question to array
}
else { // checks if question type is TF
questionsArr.add(readTF(questionType, question, points, console)); // adds TF question to array
}
}
// READS ONE TF QUESTION
public static QuestionTF readTF(String questionType, String question, int points, Scanner console) { // returns new QuestionTF object
String ans = console.nextLine();
return new QuestionTF(question, questionType, points, ans);
}
// READS ONE SA QUESTION
public static QuestionSA readSA(String questionType, String question, int points, Scanner console) {
String ans = console.nextLine();
return new QuestionSA(question, questionType, points, ans); // returns new QuestionSA object
}
// READS ONE MC QUESTION
public static QuestionMC readMC(String questionType, String question, int points, Scanner console) {
int numChoices;
String[] choices;
String ans;
numChoices = Integer.parseInt(console.nextLine());
choices = new String[numChoices];
ans = console.nextLine();
for(int i = 0; i < numChoices ; i++) {
choices[i] = console.nextLine();
}
return new QuestionMC(question, questionType, points, choices, ans); // returns new QuestionMC object
}
// STARTS QUIZ
public void quiz() {
int qCount = 0;
int ranQ;
Scanner userInput = new Scanner(System.in);
String userAns;
Random r = new Random();
// RUNS QUIZ
while (qCount < qNum) {
ranQ = r.nextInt(qNumFile);
// CHECKS IF QUESTION HAS ALREADY BEEN ASKED
if (!checkDup(ranQ, questionsAsked)) {
questionsAsked[ranQ] = true;
Question question = questionsArr.get(ranQ); // GETS RANDOM QUESTION FROM ARRAY
question.printQuestion(); // prints question and points
userAns = userInput.next(); // retrieves answer from user
// CHECKS USER'S ANSWER
if(userAns.equals("SKIP")) {
System.out.println("You have chosen to skip this question.");
}
else {
checkAnswer(userAns, question, player);
}
qCount++;
System.out.println();
}
}
}
// CHECKS IF QUESTION HAS ALREADY BEEN ASKED
public boolean checkDup(int ranQ, boolean questionsAsked[]){
return questionsAsked[ranQ];
}
// CHECKS ANSWER AND ADDS POINTS
public void checkAnswer(String userAnswer, Question question, Player player) {
if(question.checkAnswer(userAnswer)) {
System.out.println("Correct you get " + question.getPoints());
player.setScore(question.getPoints());
}
else {
System.out.println("Incorrect, the answer was " + question.getAnswer() + "." +
" you lose " + question.getPoints() + ".");
player.setScore(question.getPoints() * -1);
}
}
// Executes program
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("Please enter your first name: ");
String first = console.next();
System.out.print("Please enter your last name: ");
String last = console.next();
System.out.print("Please enter the game file name: ");
String file = console.next();
QuizBowlRedo newGame = new QuizBowlRedo(first, last, file); // creates new game of QuizBowl
newGame.quiz(); // starts game
}
}
这是我的一个对象类的构造函数.QuestionMC、QuestionSA 和 QuestionTF 扩展了 Question 类:
Here is a constructor for one of my object classes. QuestionMC, QuestionSA, and QuestionTF extend the Question class:
public QuestionSA(String question, String questionType, int points, String answer) {
super(question, questionType, points);
this.answer = answer;
}
这是我正在阅读的文本文件:
And here is the text file that I'm reading from:
3
TF 5
There exist birds that can not fly. (true/false)
true
MC 10
Who was the president of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
SA 20
What city hosted the 2004 Summer Olympics?
Athens
我正在谈论的部分从 readQuestion()
方法开始.我正在尝试将对象添加到 ArrayList
的区域中,注释为READS ONE .... QUESTION".到目前为止,它的结果是一个空的 ArrayList
.
The part I'm talking about starts at the readQuestion()
method. And I'm trying to add objects to the ArrayList
in the areas with the comments saying "READS ONE .... QUESTION". So far, it's coming out with an empty ArrayList
.
推荐答案
readFile()
循环直到 qnum 并调用readQuestion()
.但是,它不会将当前"问题编号传递给readQuestion()
.所以readQuestion()
总是从第一个开始?
readFile()
is looping until qnum and callingreadQuestion()
. However, it does not pass the "current" question number toreadQuestion()
. soreadQuestion()
always starts from the first?
readQuestion()
正在打开输入文件(因此,可能一遍又一遍地阅读第一个问题).这可能是 readFile()
readQuestion()
is openning the input file (and thus, probably read the first question over and over again). it is probably the task of readFile()
该方法的第一行从标准输入读取.您可能打算从游戏文件扫描仪读取?
The first line of the method reads from stdin. You probably meant to read from gameFile scanner?
这篇关于Java 程序不会向 ArrayList 添加对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!