i keeep getting 4 errors on lines 36,37,40 and 124. line 36 keeps giving me an e
ID: 3535720 • Letter: I
Question
i keeep getting 4 errors on lines 36,37,40 and 124. line 36 keeps giving me an error say cannot find symbol ....symbol : class player ....location : class TriviaGame .....How can i fix these errors????
Here is my code:
package triviagame;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class TriviaGame {
private static Object[] players;
public static void main(String args[], Question[] qArray) 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 [] ps;
ps = new player[NUM_PLAYERS];
for (int i = 0; i < NUM_PLAYERS; i++)
{
ps[i] = new player(i+1);
}
// Create an array to hold Question objects.
Question[] questions = new Question [NUM_QUESTIONS];
// Initialize the array with data.
initQuestions(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.
ps[playerTurn - 1].chooseAnswer();
// See if the correct answer was chosen.
if (qArray[i].getCorrectAnswerNumber() == ps[playerTurn - 1].getCurrentAnswer())
{
ps[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(ps);
}
/**
* The initQuestions method uses the contents of the trivia.text file to
* populate the qArray parameter with Question objects.
*/
public static void initQuestions(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()));
}
}
public static void displayQuestion(Question 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(players[] 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!");
}
}
}
Explanation / Answer
Hi, it looks like it is complaining about your class player/s. You have a shift in the class name (see your viod showGameResults fxn) Comment/ share your player/s class with me if you are still getting an error
binarymonkey001@gmail.com
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.