0
我有一款遊戲模擬兩個玩家擲硬幣並猜測頭部或尾部。當第一個玩家獲得5分時,遊戲會停止。我已經嘗試了一段時間的聲明,但它不會停止,直到兩者都結束了。編程新手,可以使用比我更多知識的全新眼睛。謝謝大家的幫助。 (看代碼開始//玩遊戲。)當玩家獲得5分時,無法弄清楚如何讓遊戲停止。
/**
* This program demonstrates the coin class
* and plays a game between two players.
*/
public class CoinGame
{
public static void main(String[] args)
{
final int maxPoints = 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.print("Enter the first player's name: ");
player1Name = keyboard.nextLine();
System.out.print("Enter the second player's name: ");
player2Name = keyboard.nextLine();
// Create the coin.
Coin coin = new Coin();
// Create the two players.
Player player1 = new Player(player1Name);
Player player2 = new Player(player2Name);
// Play the game.
while (player1.getPoints() <= maxPoints || player2.getPoints() <= maxPoints)
{
System.out.println("----------------------------");
// System.out.printf("Now playing round %d.\n", round + 1);
// Flip the coin.
coin.toss();
// The players make their guesses.
player1.makeGuess();
player2.makeGuess();
// Determine the winner of this round.
roundResults(coin, player1, player2);
}
// Display the grand winner.
displayGrandWinner(player1, player2);
}
/**
* The roundResults method determines the results of
* the current round. The parameters are:
* coin: The Coin object
* player1: Player #1 object
* player2: Player #2 object
*/
public static void roundResults(Coin coin, Player player1,
Player player2)
{
// Show the coin side.
/*System.out.printf("The coin turned up %d.\n",
coin.getSideUp());
*/
System.out.printf("The coin turned up: %s\n", coin.getSideUp());
// Check each player's guess and award points.
checkGuess(player1, coin);
checkGuess(player2, coin);
}
/**
* The checkGuess method checks a player's guess against
* the coin's result. The parameters are:
* player: The Player object to check.
* coin: The Coin object.
*/
public static void checkGuess(Player player, Coin coin)
{
final int POINTS_TO_ADD = 1; // Points to award winner
String guess = player.getGuess(); // Player's guess
String coinResult = coin.getSideUp(); // Heads or Tails
// 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(coinResult))
{
player.addPoints(POINTS_TO_ADD);
System.out.printf("Awarding %d point(s) to %s.\n",
POINTS_TO_ADD, player.getName());
}
}
/**
* The displayGrandWinner method displays the game's grand winner.
* The parameters are:
* player1: Player #1
* 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 grand winner!");
else if (player2.getPoints() > player1.getPoints())
System.out.println(player2.getName() + " is the grand winner!");
else
System.out.println("Both players are tied!");
}
}
謝謝,就是這樣。我也不得不刪除=所以它會停在5. – Naro
是的,這是對的。 –