Java程序不会向ArrayList添加对象

我正在尝试将不同的对象添加到ArrayList ,但它无法正常工作。 我不确定是不是因为我正在错误地添加对象或者是否有其他错误。

这是我的代码。

 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 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(); 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类:

 public QuestionSA(String question, String questionType, int points, String answer) { super(question, questionType, points); this.answer = answer; } 

这是我正在阅读的文本文件:

 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

  1. readFile()循环到qnum并调用readQuestion() 。 但是,它不会将“当前”问题编号传递给readQuestion() 。 所以readQuestion()总是从第一个开始?

  2. readQuestion()打开输入文件(因此,可能一遍又一遍地读取第一个问题)。 它可能是readFile()的任务

  3. 该方法的第一行从stdin读取。 你可能想从gameFile扫描仪读取?