lundi 30 novembre 2020

[Beginner]How do you make the game start again when the user exit by entering -1?

I'm trying to make a Hi-Lo guessing game, and I've been struggling to find a way to make the game start again when the user enters '-1'.

I have just started to learn java by myself, so please let me know if you have any suggestions or advice to improve the code I made. I would really appreciate your help.

Game explanations

1)The game generates a random number between 0 and 100, and the user has to guess it. If the guess is bigger or smaller than the random number, it displays messages.

2)If the user enters a value out of range, the game displays a warning message.

3)If the guess is correct, the game gives an option asking if the user wants to play it again.

★★4)Here, when the user enters a certain value (I set it to -1), I want the game to end, but also want to give an option to start the game again.★★


//import random number generator and scanner
import java.util.Random;
import java.util.Scanner;

    public class HighLowGame {

    public static void main(String[] args) {

    //create scanner, variables and a random number
    Scanner scanner = new Scanner (System.in);
    Random generator = new Random();
    int number =generator.nextInt(100) + 1;
    int count = 0;
    boolean game = true;

    //prompt user to enter a number
    System.out.println("Please guess the number.(Enter -1 to quit): ");

    while(game){

        //user enters a number
        int guess = scanner.nextInt();

        //if user enters -1, the game ends
        if(guess == -1){
            break;
        }

        //guessed number is out of range
        if(guess>100 || guess<0){
            System.out.println("The number should be between 0 and 100.");
        }

        //guessed number is smaller than the random number
        if(guess < number && 0 <= guess && guess <= 100 ){
            count++;
            System.out.println("That is too low. Please try again.(Enter -1 to quit):");
        }

        // guessed number is bigger than the random number
        else if(guess > number && 0 <= guess && guess <= 100){
            count++;
            System.out.println("That is too high. Please try again.(Enter -1 to quit):");
        }

        //guessed number is the same as the random number
        else if(guess==number) {
            count++;

            //displays the message and the attempt count
            System.out.println("Congratulations! You got the correct number.");
            System.out.println("Your attempt was " + count + " tries.");

            // ask the user if they want to play the game again
            count = 0;
            System.out.println("Would you like to play the game again?(yes/no): ");
            String another = scanner.next();

            // if the user wants to end the game, it ends
            if (another.equalsIgnoreCase("no")) {
                break;
            }

            // if the user wants to play the game one more time, it starts again
            else {
                number = generator.nextInt(100) + 1;
                System.out.println("Please guess the number(Enter -1 to quit): ");
                }
            }

       }

    }

 }

Aucun commentaire:

Enregistrer un commentaire