2016-03-13 44 views
0

我試圖將不同的對象添加到ArrayList,它不工作。我不確定是否因爲我錯誤地添加了對象或者是否有其他錯誤。Java程序不會將對象添加到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<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擴展問題類:

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()方法談論開始。並且我試圖在有「READS ONE .... QUESTION」這樣的註釋的地方添加對象到ArrayList。到目前爲止,它出來了一個空的ArrayList

+0

你會得到什麼錯誤?總是包含你得到的錯誤。 –

+0

沒有彈出的實際錯誤;該程序編譯。我唯一的問題是將對象添加到ArrayList。 – Jasmine

+0

Question,QuestionTF,QuestionSA,QuestionMC是什麼關係? – Learner

回答

0
  1. readFile()正在循環,直到qnum並致電readQuestion()。但是,它不會將「當前」問題編號傳遞給readQuestion()。所以readQuestion()總是從第一個開始?

  2. readQuestion()正在打開輸入文件(因此可能會一遍又一遍地讀取第一個問題)。它可能是readFile()的任務

  3. 該方法的第一行從stdin讀取。你可能打算從gameFile掃描儀讀取?

+0

我對你的前兩條評論有點困惑。對於第一個,你是說我應該做一些像'readQuestion(i)'?如果您通過「當前」問題編號,我不確定它應該是什麼。另外,當你說'readQuestion()'正在打開輸入文件時,我不知道你的意思。 – Jasmine

+0

問問你自己這個問題 - readQuestion如何「知道」它讀的是哪個問題?當它被第二次調用(大概是讀第二個問題),它應該怎麼知道這是第二次被調用? –

+0

必須是我邏輯中的錯誤,我認爲'掃描器gameFile'會將指針放在任何地方。因此,指針會通過循環不斷向下移動文件。 – Jasmine