lundi 4 novembre 2019

The Guessing Game

Goal: Create a Guessing game with random number from 1 - 100.
If you guess the correct number the program will end and say "You win!".
If you guess the wrong number the program will ask if you want to try again, yes if you want and no will terminate the program.

I am having a problem on how will I loop it back to the "Guess the number from 1 -100: "

    Random rand = new Random();
    int numberToGuess = rand.nextInt(100);
    Scanner input = new Scanner(System.in);
    int guess, yes = 1;
    char a;
    System.out.println("~ The Guessing Game ~");
    System.out.print("Guess the number from 1 - 100: ");
    guess = input.nextInt();
    if(guess!=numberToGuess){
       System.out.println("You lose");
       System.out.print("Try again(yes/no)? ");
       a = input.next().charAt(0);
        if(a==1){
            //needs to loop to ask again....
        }
        else{
            System.exit(0);
        }
    }
    else{
        System.out.println("You Win!");

    }

Aucun commentaire:

Enregistrer un commentaire