0

I'm new to Java programming and taking a college course where I have an assignment to create a Hi/Lo guessing game. The game provides up to 5 attempts for the user to input a number between 1 and 100 (inclusive). The program must provide the logic back of whether the answer is too low, too high or correct. The program must provide the option to play again after either winning or the 5 failed attempts.

I've recreated this program about 10 times :(. I cannot get he logic to work to follow the instructions above. I cannot stop the tries at 5 attempts... and I cannot get the program to execute a new game.

Any help is greatly appreciated. I've spent countless hours writing and re-writing this code with MANY different results - but not the intended ones.

This is my first time posting so, I apologize if the format to post is not correct.

I've looked through more forums and examples than I care to admit and none of code I've reviewed and tried implementing have given me the results of limiting the user input to 5 tries each time and ability to play again multiple times.

Here is my code:

public class HiLoGuessingGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Initialize scanner and random number gennerator
        Scanner input = new Scanner(System.in); 
        Random generator = new Random();

        //State the rules of the game 
        System.out.println("The Hi-Lo Guessing Game. Guess a number between 1-100");
        System.out.println("You have 5 attempts!");

       /* define the variable Guess (user iput)
          define the variable Answer (random generator)
          define the variable Counter (track number of tries and limit to 5)
          define the variable PlayAgain (Y/N question)*/
        int guess = 0; 
        int answer = generator.nextInt(100)+1;
        int counter = 1;
        String playAgain;
        boolean gameOver = false;

        //Ask the Hi-Lo question - pick number 1-100 (inclusive) 
        //Provide feedback answer too high, too low or you win! 
        //Limit number of tries in the game to 5 

        while (guess != answer) {
            System.out.print("Enter your guess: ");
            guess = input.nextInt(); 
            counter++;

            if (guess < answer)  {
                 System.out.println("Your guess " + guess + " is too low. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess > answer) {
                 System.out.println("Your guess " + guess + " is too high. Try again");
                 System.out.println("This is attempt: " + counter);
            } else if (guess == answer) {
                 System.out.println("Your guess " + guess + " is correct! You win!");
                 System.out.println();
                 System.out.println("Would you like to play again (Y/N)?");
                 playAgain = input.next();
            } 

        }
        if (counter ==6) {
            System.out.println("Sorry, you've reached your max atttempts.");
            System.out.println("Would you like to play again (Y/N)?"); 
            playAgain = input.next();
        }

        // Play again logic
        boolean isValid;
        do {
            System.out.print("Would you like to play again (Y/N)?");
            playAgain = input.next().toUpperCase();
            isValid = playAgain.equals("Y") || playAgain.equals("N");
            playAgain = input.next();
            counter = 1;
            if ( !isValid ) {
                 System.out.println("Error, please enter Y or N");
                 System.out.println();
            }
        } while (!isValid);
    }
}
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
  • 1
    You must check the counter inside your while-loop not after it. – Ralf Renz Nov 22 '19 at 09:01
  • `while (guess != answer && counter != 6)` Like that your while loop will stop if one of those condition is not verified. You can let your `if (counter == 6) {...}` to keep the print logic in the case the player looses but you should remove the second System.out.println and the input read as you'll do it in the do...while block – Imaguest Nov 22 '19 at 09:11
  • Thank you. I made those changes and they seem to work now. Only problem I still have is getting the logic to play again to work :) – Grace Sensebe Ramon Nov 22 '19 at 10:05

1 Answers1

0

You can add an extra condition to your while-loop:

while (guess != answer && counter < 5) {
    // ...
}

Alternatively, you can break the loop when you get a right answer:

 while (counter < 5) {
    // ...
    if (answer == guess){
       // ...
       break;
    } 
 } 
Henrik Klev
  • 322
  • 1
  • 11
  • Thank you for the reply. I created the break loop - since that makes sense. and I also tried the counter < 5 logic and that worked too. Thank you! – Grace Sensebe Ramon Nov 22 '19 at 10:06
  • I was able to get the retry the max attempts to stop at 5 - THANK YOU! Now, I'm working on the retry (playAgain) logic and I still can't get that successfully to work. below is part of the code. I don't know how to modify my original post - sorry. this is my first time ever posting here: – Grace Sensebe Ramon Nov 22 '19 at 11:07
  • I wrapped the whole block in a new while statement while(playing == true) and then ended with: // Play again logic System.out.print("Would you like to play again (Y/N)?"); String play = input.nextLine(); while(playing = play.equalsIgnoreCase("y")) { System.out.println(); guess = input.nextInt(); guess = 0; answer = generator.nextInt(100)+1; counter = 0; – Grace Sensebe Ramon Nov 22 '19 at 11:08