java - 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 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 number again:");                 age = scan.nextint();             }            }        } } 

so made classification of child base on number(s) of days.

how can simplify code because if user enter number out of range program once prompt user enter value, if user enter number out of range once again program stop prompt user enter number because if put conditional construct under else make code longer problem how make code shorter program continue prompt user if entered number out of range.

sample output enter child's number(s) of days: 121 classification: out of range enter number again: // program prompt user again -1 //but program not print out of range /how make program prompt user enter number again , make program shorter/

you should become familiar while-loop.

it loops long 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 side note, java convention start class names capital letter (child -> child).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -