Amben Uchiha
Amben Uchiha

Reputation: 463

How to make a program prompt the user continuously?

public class child {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int AGE;
        System.out.println("Enter Child's number(s) of Days: ");
        AGE = scan.nextInt();

        if (AGE == 0 || AGE == 1){
            System.out.println("Classification: New Born");
        }
        else if (AGE >= 2 && AGE <= 10){
            System.out.println("Classification: Infant");
        }
        else if (AGE > 9 && AGE < 19){
            System.out.println("Classification: New Born");
        }
        else if (AGE > 17 && AGE < 37){
            System.out.println("Classification: TODDLER");
        }
        else if (AGE > 36 && AGE < 120 ) {
            System.out.println("Classification: KID");
        }
        else{
            System.out.println("Classification: Out of Range");
            System.out.println("Enter a number again:");
            AGE = scan.nextInt();
            if (AGE == 0 || AGE == 1){
                System.out.println("Classification: New Born");
            }
            else if (AGE >= 2 && AGE <= 10){
                System.out.println("Classification: Infant");
            }
            else if (AGE > 9 && AGE < 19){
                System.out.println("Classification: New Born");
            }
            else if (AGE > 17 && AGE < 37){
                System.out.println("Classification: TODDLER");
            }
            else if (AGE > 36 && AGE < 120 ) {
                System.out.println("Classification: KID");
            }           
            else{
                System.out.println("Classification: Out of Range");
                System.out.println("Enter a number again:");
                AGE = scan.nextInt();
            }   
        }   
    }
}

So i made a classification of a child base on their number(s) of days.

how can i simplify this code because if the user enter a number that is out of range the program will once prompt the user to enter a value, but if the user enter a number that is out of range once again the program now will stop to prompt the user to enter another number because if i put another conditional construct under else it will make my code longer so my problem is how do i make my code shorter but the program will continue to prompt the user if the entered number is out of range.

Sample output Enter Child's number(s) of Days: 121 Classification: Out of Range Enter a number again: // the program will prompt the user again -1 //but now the program now will not print out of range /how to i make the program to prompt the user to enter a number again and make my program shorter/

Upvotes: 2

Views: 177

Answers (2)

Luciano van der Veekens
Luciano van der Veekens

Reputation: 6577

You should become familiar with a while-loop.

It loops for as long as the condition remains true after each iteration.

Scanner scan = new Scanner(System.in);

while(true) {
    System.out.println("Enter Child's number(s) of Days: ");
    int AGE = scan.nextInt();

    if (AGE == 0 || AGE == 1) {
        System.out.println("Classification: New Born");
    } else if (AGE >= 2 && AGE <= 10) {
        System.out.println("Classification: Infant");
    } else if (AGE > 9 && AGE < 19) {
        System.out.println("Classification: New Born");
    } else if (AGE > 17 && AGE < 37) {
        System.out.println("Classification: TODDLER");
    } else if (AGE > 36 && AGE < 120) {
        System.out.println("Classification: KID");
    } else {
        System.out.println("Classification: Out of Range");
    }
}

On a side note, the Java convention is to start class names with a capital letter (child -> Child).

Upvotes: 7

Tayyab
Tayyab

Reputation: 1217

public class child {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int AGE;
        while(true){
            System.out.println("Enter Child's number(s) of Days: ");
            try {
                AGE = scan.nextInt(); 
            } catch (Exception e) {
                System.out.println ("Please enter a number!");
            }            

            if (AGE == 0 || AGE == 1){
                System.out.println("Classification: New Born");
            }
            else if (AGE >= 2 && AGE <= 10){
                System.out.println("Classification: Infant");
            }
            else if (AGE > 9 && AGE < 19){
                System.out.println("Classification: New Born");
            }
            else if (AGE > 17 && AGE < 37){
                System.out.println("Classification: TODDLER");
            }
            else if (AGE > 36 && AGE < 120 ) {
                System.out.println("Classification: KID");
            }
            else{
                System.out.println("Classification: Out of Range");
            }
        }
    }
}

Hope this helps.

Upvotes: 1

Related Questions