jeudi 24 septembre 2015

How to repeat "if" statement when output is false

I'm a beginner at java and am taking a class at school to learn it. I was messing around with if statements to see if I can create a simple game that you have to guess what the number is from a random generator. Well, I have all the code set up except for that fact that if the guess is too high or too low I don't know how to allow them to re-enter a number and keep playing until they get it. It just stops; here is the code:

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

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Random rand = new Random();

        int random = rand.nextInt(10) + 1;

        System.out.print("Pick a number 1-10: ");
        int number = input.nextInt();

        if (number == random) {
            System.out.println("Good!");
        } else if (number > random) {
            System.out.println("Too Big");
        } else if (number < random) {
            System.out.println("Too Small");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire