Home > Back-end >  Basic Java HiLow guessing game
Basic Java HiLow guessing game

Time:11-30

I am fully aware this question has been asked many times, it is a classic first year problem in CSC. I am not looking for the solution to the problem itself. I think I have it basically done however I am missing something that I cannot find how to do.

Here is my code:

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

public class HiLow
{
    public static void main (String[] args) 
    {
        Random generator = new Random();
        Scanner scan = new Scanner(System.in);
        int num1,guess;
        int count = 0;
        num1 = generator.nextInt(100)   1;
        while(true) {
            System.out.print("Enter an integer between 1 or 100 or enter 0 at anytime to quit: ");
            guess = scan.nextInt();
            count  ;
            if(guess == num1 || guess == 0) {
                if(guess == 0) {
                    System.out.println("Thanks for playing");
                    break;
                }
                System.out.println("Congrats you've guessed correct and your total guesses is "   count );
                break;
            }
            else if (guess > 100 || guess < 1) {
                System.out.print("I see you cannot follow instructions. I said ");
                count--;
            }
            else if (guess > num1) {
                System.out.println("You have guessed too high. ");
            }
            else {
                System.out.println("You have guessed too low.");
            }
        }
    }
}

My problem is i am required to prompt the user at the point of "if the user quits or successfully guesses the correct number, prompt the user to see if they wish to play again". I am lost and not sure how to continue my while loop from the beginning after my breaks. Is there a way to end the break condition i have from (guess == num1 || guess ==0) and direct my program to start again at the while(true) statement?

Thanks

CodePudding user response:

I will say search up continue;

Tips to help further:

  1. The continue statement is used to bring the loop back to the start, try it instead of a break where you want the user to continue.

  2. You need some sort of check if the user wants to continue, (try asking them to type in some specific int you check, p.s negative numbers are integers as well)

CodePudding user response:

@Ahmed thinks you should continue, I would rather not break, or conditionally break.

CodePudding user response:

Well there are multiple ways you could accomplish this, One would be to just to prompt the user with a "press q to quit" dialogue using the Scanner class where .next() returns the String when the user hits enter:

  if(guess == num1 || guess == 0) {
            if(guess == 0) {
                System.out.println("Thanks for playing");
            }else{
                System.out.println("Congrats you've guessed correct and your total guesses is "   count );
                
            }
            System.out.println("would you like to play again [y/n]?");
            if(scan.next().equals("y")){
              num1 = generator.nextInt(100)   1;
            }else{
              break;
            }
        }

If thats what you mean. Hopefully I helped. or maybe you can have it only quit at zero, if so just remove that second break and replace it with num1 = generator.nextInt(100) 1; to set the new value to guess.

  • Related