2015-12-01 181 views
-4

我遇到了一些麻煩,我非常感謝一些幫助。當談到java時,我仍然是一個「noobie」,所以請理解我可能會犯一些愚蠢的錯誤。無論如何,我正在嘗試製作一款雙人Java遊戲,並且不斷收到我不明白的錯誤。我有三個不同的課程。這是我的主類爪哇2玩家瑣事遊戲

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]; 

} 

} 

如果有任何幫助,你們可以提供,我將不勝感激。我似乎無法弄清楚爲什麼我不斷收到阻止它運行的錯誤。謝謝,麻煩您了。

這些都是我得到的錯誤,我不知道這是你的意思,我發現了什麼我早些時候曾是從別的

異常在線程「主要」 java.lang.Error的:未解決編制問題: 方法intQuestion(問題[])是未定義的類型Assignment3 qArray不能被解析爲一個變量 qArray不能被解析爲一個變量

at Assignment3.main(Assignment3.java:42) 
+0

你有什麼錯誤?你可以發佈你得到的堆棧跟蹤嗎? – ryekayo

+1

請問您能合理縮進您的代碼嗎? – khelwood

+0

錯誤在哪裏? – hbelmiro

回答

0

你的方法被命名爲initQuestion,但你正在呼叫intQuestion

哦,並且您正在使用initQuestion之外的qArray,這是看起來被定義的唯一地方。

+0

我只是對downvote很好奇 - 有什麼我可以改進我的回答? – stuXnet

+0

我沒有downvote它,idk爲什麼其他人做了,它幫助了一點,所以謝謝,即時通訊仍在努力試圖讓它工作 – user211212