【问题标题】:Java 2 player trivia gameJava 2 玩家琐事游戏
【发布时间】:2015-12-01 19:39:56
【问题描述】:

我遇到了一些麻烦,我将非常感谢一些帮助。当谈到 java 时,我仍然是一个“noobie”,所以请理解我可能会犯愚蠢的错误。无论如何,我正在尝试制作一个两人 Java 游戏,但我不断收到我不理解的错误。我有三个单独的课程。这是我的主要课程

import java.io.File;


import java.io.IOException;
 import java.util.Scanner;

 public class Assignment3

 {
 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.
 questions[] questions = new questions [NUM_QUESTIONS];


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


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

 {
 // Display the question.
 Assignment3.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 initQuestions method uses the contents of the trivia.txt file to

 * populate the qArray parameter with Question objects.

 */

 public static void initQuestions(questions 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 questions();


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

 }
 }


 public static void displayQuestion(questions q, int playerNum)

 {

 // Display the player number.
 System.out.println("Question for player #" + playerNum);

 System.out.println("------------------------");


 // Display the question.
 System.out.println(q.getQuestionText());

 for (int i = 1; i <= 4; i++)

 {
 System.out.println(i + ". " + q.getPossibleAnswer(i));

 }

 }

 public static void showGameResults(player[] players)

 {

 // Display the stats.
 System.out.println("Game Over!");

 System.out.println("---------------------");

 System.out.println("Player 1's points: " + players[0].getPoints());

 System.out.println("Player 2's points: " + players[1].getPoints());


 // Declare the winner.
 if (players[0].getPoints() > players[1].getPoints())

 System.out.println("Player 1 wins!");

 else if (players[1].getPoints() > players[0].getPoints())

 System.out.println("Player 2 wins!");

 else

 System.out.println("It's a TIE!");
 }
 }

这是我的播放器类

import java.util.Scanner;
 public class player

 {

 private int playerNumber; // The player number
 private int points; // Player's points
 private int currentAnswer; // Current chosen answer


 //Constructor
 public player(int playerNum)

 {

 playerNumber = playerNum;

 points = 0;

 }

 public void chooseAnswer()

 {
 // Create a Scanner object for keyboard input.
 // Get the user's chosen answer.


 Scanner keyboard = new Scanner(System.in);

 System.out.print("Please enter your Answer"); //Asks user for a number

 this.currentAnswer = keyboard.nextInt();

 }

 public int getCurrentAnswer()

 {

 return this.currentAnswer; //Returns Current Answer

 }

 public void incrementPoints()

 {

 this.points++; //Increments the points

 }

 public int getPoints()

 {

 return this.points; //Returns the points

 }
 } 

这是我的问题课

public class questions
 {
 // Constant for the number of answers
 public final int NUM_ANSWERS = 10;

 // The trivia question
 private String questionText;

 // An array to hold possible answers.
 private String possibleAnswers[] = new String[NUM_ANSWERS];

 // The number (1, 2, 3, or 4) of the correct answer.
 private int correctAnswer;


 //Constructor
 public questions()
 {
 // Initialize all fields to "" or 0;
 questionText = "";

 correctAnswer = 0;

 for (int i = 1; i < NUM_ANSWERS; i++)

 setPossibleAnswer("", i);
 }

 public void setQuestion(String question)
 {
 //Sets the question
 this.questionText = question; 

 }

 public void setPossibleAnswer(String text, int num)

 {
 //Sets possible Answer
 this.possibleAnswers[num] = text; 

 }

 public void setCorrectAnswerNumber(int num)

 {
 //Sets correct Answer
 this.correctAnswer = num; 

 }


 public String getQuestionText()

 {
 //Returns Question Text
 return this.questionText; 

 }

 public String getPossibleAnswer(int num)

 {
 //Returns Possible Amswer
 return this.possibleAnswers[num]; 
 }

 public int getCorrectAnswerNumber()

 {
 //Returns Correct Answer
 return this.correctAnswer; 

 }

 public String getCorrectAnswer()

 {
 //Returns Possible Answer
 return this.possibleAnswers[this.correctAnswer]; 

 }

 }

如果你们能提供任何帮助,我将不胜感激。我似乎无法弄清楚为什么我不断收到这些阻止它运行的错误。请和谢谢。

这些是我得到的错误,我不知道这是不是你的意思,我发现我之前的错误来自其他东西

线程“main”java.lang.Error 中的异常:未解决的编译问题: 方法 intQuestion(questions[]) 未为类型 Assignment3 定义 qArray 无法解析为变量 qArray 无法解析为变量

at Assignment3.main(Assignment3.java:42)

【问题讨论】:

  • 您遇到什么错误?你能发布你得到的堆栈跟踪吗?
  • 你能合理地缩进你的代码吗?
  • 成为“noobie”并不能成为您提供实际错误和不格式化代码的借口
  • 您有一些严重的格式问题,并且没有使用正确的语法。如果你在第 6 章,你真的需要确定可编译的代码格式。
  • 始终在编写代码时进行编译,并检查以确保特定实例正常工作。你不应该从一开始就有一个错误列表。

标签: java eclipse


【解决方案1】:

您的方法名为initQuestion,但您调用的是intQuestion

哦,您在initQuestion 之外使用qArray,它似乎是唯一被定义的地方。

【讨论】:

  • 我只是对否决票感到好奇——我的回答有什么可以改进的吗?
  • 我没有投反对票,我不知道为什么其他人这样做,它帮助了一点,所以谢谢你,我仍在努力让它工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多