Naro Naro - 1 year ago 64
Java Question

Can't figure out how to get the game to stop when a player scores 5 points.

I have a game that simulates two players flipping a coin and guessing heads or tails. The game is suppose to stop when the first player gets 5 points. I have tried a while statement but it doesn't stop till both are over 5. New to programming and could use a fresh set of eyes with more knowledge than me. Thank you all for any help. (look at the code starting at //play the game.)

* 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(;

// 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.printf("Now playing round %d.\n", round + 1);

// Flip the coin.

// The players make their guesses.

// 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",
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))
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("Game over. Here are the results:");
System.out.printf("%s: %d points.\n", player1.getName(),
System.out.printf("%s: %d points.\n", player2.getName(),

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!");
System.out.println("Both players are tied!");


Answer Source


while (player1.getPoints() <= maxPoints || player2.getPoints() <= maxPoints)


while (player1.getPoints() <= maxPoints && player2.getPoints() <= maxPoints)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download