我正在创建骰子游戏模拟,但遇到以下错误:
exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method displayGrandWinner(Player, Player) is undefined for the type ChoHan
我将在下面发布该类的所有代码,因为我不确定错误在哪里,但是我认为唯一必要的代码是displayGrandWinner
方法:
导入java.util.Scanner;
public class ChoHan {
public static void main(String[] args) {
final int MAX_ROUNDS = 5; // Number of rounds
String player1Name; // First player's name
String player2Name; //Second player's name
// Create a Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//Get the player's names
System.out.println("Enter the firs tplayers name:");
player1Name = keyboard.nextLine();
System.out.println("Enter the second players name:");
player2Name = keyboard.nextLine();
//Create the dealer
Dealer dealer = new Dealer();
//Create the two players
Player player1 = new Player(player1Name);
Player player2 = new Player(player2Name);
//play the rounds
for(int round = 0; round < MAX_ROUNDS; round++){
System.out.println("--------------------------");
System.out.printf("Now playing round %d.\n", round+1);
//roll the dice
dealer.rollDice();
//the players make their guesses
player1.makeGuess();
player2.makeGuess();
//determine the winner of this round
roundResults(dealer, player1, player2);
}
//Display the grand winner
displayGrandWinner(player1, player2);
}
/**
* The roundResults method determines the results of the current round
* @param dealer The Dealer object
* @param player1 Player #1 object
* @param player2 Player #2 object
*/
public static void roundResults(Dealer dealer, Player player1, Player player2){
//Show the dice values
System.out.printf("The dealer rolled %d and %d.\n", dealer.getDie1Value(), dealer.getDie2Value());
System.out.printf("Result: %s\n", dealer.getChoOrHan());
//Check each players guess and award points
checkGuess(player1, dealer);
checkGuess(player2, dealer);
}
/**
* The checkGuess method checks a player's guess against the dealer's result.
* @param player The Player object to check
* @param dealer The Dealer object
*/
public static void checkGuess(Player player, Dealer dealer){
final int POINTS_TO_ADD = 1; //points to award the winner
String guess = player.getGuess(); //player's guess
String choHanResult = dealer.getChoOrHan(); //Cho or Han
//display the player's guess
System.out.printf("The player %s guessed %s\n", player.getName(), player.getGuess());
//award points if the player guessed correctly
if(guess.equalsIgnoreCase(choHanResult)){
player.addPoints(POINTS_TO_ADD);
System.out.printf("Awarding %d points to %s\n", POINTS_TO_ADD,player.getName());
}
/**
* The displayGrandWinner method displays the games grand winner
* @param player1 Player #1
* @param player2 Player #2
*/
public static void displayGrandWinner(Player player1, Player player2){
System.out.println("----------------------------");
System.out.println("Game over. Here are the results:");
System.out.printf("%s: %d points.\n", player1.getName(), player1.getPoints());
System.out.printf("%s: %d points.\n", player2.getName(), player2.getPoints());
if(player1.getPoints() > player2.getPoints())
System.out.println(player1.getName() +" is the winner");
else if(player2.getPoints() > player1.getPoints())
System.out.println(player2.getName() +" is the winner");
else
System.out.println("Tie");
}
}
}
为什么displayGrandWinner
方法没有注册为ChoHan
类的一部分?