JAVA Trivia 游戏数组超出范围

标签 java arrays arraylist

我知道问题是编译器说代码中实际玩游戏的 qArray 部分有错误,有一条注释说玩游戏。我认为 qArray 的范围位于使用它的循环之外,但我不确定如何从 intQuestions 方法获取数据到需要的位置妨碍我使用它。或者我应该将问题数组放在 qArray 为 @ 的位置?

所需的输出是调用 intQuestions 方法,然后将数据传递到 for 循环中使用的数组,其中注释说玩游戏!我知道 qArray 的范围在外部,但是我如何将数据从数组传递到它所说的玩游戏的位置以及我使用它的方式?

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

 }
 }

最佳答案

您可能应该在 main() 方法中使用 questions 而不是 qArray:qArray 仅在内部可见intQuestions() 方法。当您使用参数 questions 调用 intQuestions() 方法时,您将数组传递给该方法并对其进行一些初始化。 intQuestions() 方法完成后,您的 questions 数组将使用一些值进行初始化。

关于JAVA Trivia 游戏数组超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526495/

相关文章:

c++ - 将模板 typedef 的数组传递给构造函数

java - 为堆栈实现迭代器

java - 更改类文件时重新启动tomcat?

java - LWJGL 改变顶点之间线条的颜色

java - 使用 MyBatis 进行批量预缓存

c - 我不明白为什么这个循环会卡住

java - 将 Eclipse 中的现有源代码包含到 Android Studio 中

java - 在 Java 中将数组添加到队列(指针问题?)

Java 8 在stream().map()中定义的函数

java - 为什么final变量没有内部类就返回?