Bobby Joes
Bobby Joes

Reputation: 1

JAVA Trivia Game Array outside scope

I know the problem is that the compiler says there is an error with the qArray part of where the game is actually played in the code there is a comment that says play the game. I think it's that the scope of qArray is outside the loop that uses it but I'm not sure how to get the data from the intQuestions method up to where it needs to be in the way I'm using it. Or should I be putting the questions array where the qArray is @?

The desired output is to have the intQuestions method called on then the data passed into the array used in the for loop where the comment says play the game! I understand the scope of qArray is outside but how would I pass the data from the array up to where it says play the game and in the way I'm using it?

 public class TriviaGame {

   public static void main(String args[]) throws IOException
   {

     // Constants
     final int NUM_QUESTIONS = 10;
     final int NUM_PLAYERS = 2;


     // Variables
     int playerTurn = 1; // The current player
     int questionNum; // The current question number
     int playerAnswer; // The player's chosen answer
     int player1points = 0; // Player 1's points
     int player2points = 0; // Player 2's points


     // Create an array of Player objects for player #1 and player #2.
     Player[] players = new Player[NUM_PLAYERS];

     for (int i = 0; i < NUM_PLAYERS; i++)

     {

        players[i] = new Player(i+1);

     }


     // Create an array to hold Question objects.
     Question[] questions = new Question [NUM_QUESTIONS];


     // Initialize the array with data.
     intQuestions(questions);


     // Play the game.
     for (int i = 0; i < NUM_QUESTIONS; i++)

     {
        // Display the question.
        TriviaGame.displayQuestion(qArray[i], playerTurn);


        // Get the player's answer.
        players[playerTurn - 1].chooseAnswer();


        // See if the correct answer was chosen.
        if (qArray[i].getCorrectAnswerNumber() == players[playerTurn - 1].getCurrentAnswer())

        {

            players[playerTurn -1].incrementPoints();

        }


        // See if the the player chose the wrong answer.
        // do nothing
        // Switch players for the next iteration.

        if (playerTurn == 1)

            playerTurn = 2;

        else

            playerTurn = 1;

     }

     // Show the game results.
     showGameResults(players);
 }

 /**

 * The intQuestions method uses the contents of the trivia.txt file to

 * populate the qArray parameter with Question objects.

 */

 public static void intQuestions(Question qArray[]) throws IOException
 {

   // Open the trivia.txt file.

   File file = new File("trivia.txt");

   Scanner inputFile = new Scanner(file);


   // Populate the qArray with data from the file.
   for (int i = 0; i < qArray.length; i++)

   {

     // Create a Question object in the array.
     qArray[i] = new Question();


     // Get the question text from the file.
    qArray[i].setQuestion(inputFile.nextLine());


    // Get the possible answers.
    for (int j = 1; j <= 4; j++)

    {

       qArray[i].setPossibleAnswer(inputFile.nextLine(), j);

    }

    // Get the correct answer.
    qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));

 }
 }

Upvotes: 0

Views: 861

Answers (1)

Yaroslav Rudykh
Yaroslav Rudykh

Reputation: 823

You should probably use questions instead of qArray in main() method: qArray is visible only inside intQuestions() method. When you invoke intQuestions() method with parameter questions you pass the array to the method and make some initialization to it. After the method intQuestions() completes your questions array will be initialized with some values.

Upvotes: 1

Related Questions