lundi 29 novembre 2021

Basic Java HiLow guessing game

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 00 at anytime to quit: ");
            guess = scan.nextInt();
            count++;
            if(guess == num1 || guess == 00) {
                if(guess == 00) {
                    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

Aucun commentaire:

Enregistrer un commentaire