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
是的,这是对的。 –