Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am working on a hi-lo guessing game in java. This is what I have so far. I nee

ID: 642434 • Letter: I

Question

I am working on a hi-lo guessing game in java. This is what I have so far. I need there to be a limit of 3 attempts when guessing, however, the problem is, if I guess the right number ON THE THIRD ATTEMPT, it is displaying a loss. I can't figure out what I have wrong.

import java.util.Scanner;
import java.util.Random;

public class HiLoGame {

    public static void main(String[] args) {
       Scanner scan = new Scanner (System.in);
       Random generator = new Random ();

      
       int userChoice;
       int guessCount;
       boolean playAgain = true;
       boolean playerWon = false;
       int gamesWon = 0;

       while (playAgain) {
           guessCount = 1;
           int computerChoice = generator.nextInt(10) + 1;
           System.out.print(" ---------Debug Line----------- computer chose " + computerChoice);
           System.out.println(" Guess a number between 1 and 10: ");
           userChoice = scan.nextInt();
           scan.nextLine();

           while (playerWon == false && guessCount < 3) {
               if (userChoice < computerChoice) {
                   System.out.println("Your guess was too low. Guess again:");
                   userChoice = scan.nextInt();
                   scan.nextLine();
                   guessCount++;
                   playerWon = false;
               }
               else if (userChoice > computerChoice) {
                   System.out.println("Your guess was too high. Guess again: ");
                   userChoice = scan.nextInt();
                   scan.nextLine();
                   guessCount++;
                   playerWon = false;
               }
               else if (userChoice == computerChoice){
                   System.out.println("Your guess was correct!");
                   gamesWon++;
                   playerWon = true;
               }
           }
           if (guessCount >= 3 && playerWon == false)
               System.out.println("Sorry! You Lost!");
           System.out.println(" Would you like to play again? (yes/no): ");
           String input = scan.nextLine();
           if (input.equalsIgnoreCase("no"))
               playAgain = false;
           else if (input.equalsIgnoreCase("yes"))  
               playAgain = true;
               playerWon = false;  

          
       }
           System.out.println("You won " + gamesWon + " games.");
   }
}

Explanation / Answer

import java.util.Scanner;
import java.util.Random;
public class HiLoGame {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Random generator = new Random ();
  
int userChoice;
int guessCount;
boolean playAgain = true;
boolean playerWon = false;
int gamesWon = 0;
while (playAgain) {
guessCount = 1;
int computerChoice = generator.nextInt(10) + 1;
System.out.print(" ---------Debug Line----------- computer chose " + computerChoice);
System.out.println(" Guess a number between 1 and 10: ");
userChoice = scan.nextInt();
scan.nextLine();
while (playerWon == false && guessCount < 3) {
if (userChoice < computerChoice) {
System.out.println("Your guess was too low. Guess again:");
userChoice = scan.nextInt();
scan.nextLine();
guessCount++;
playerWon = false;
}
else if (userChoice > computerChoice) {
System.out.println("Your guess was too high. Guess again: ");
userChoice = scan.nextInt();
scan.nextLine();
guessCount++;
playerWon = false;
}
else if (userChoice == computerChoice){
gamesWon++;
playerWon = true;
}
}
if(userChoice == computerChoice){
   playerWon = true;
}
if (guessCount >= 3 && playerWon == false)
System.out.println("Sorry! You Lost!");
else
System.out.println("Your guess was correct!");
System.out.println(" Would you like to play again? (yes/no): ");
String input = scan.nextLine();
if (input.equalsIgnoreCase("no"))
playAgain = false;
else if (input.equalsIgnoreCase("yes"))
playAgain = true;
playerWon = false;
  
}
System.out.println("You won " + gamesWon + " games.");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote