java - how do i prompt a user to re enter their input value, if the value is invalid? -
public int inputnumber() { scanner input = new scanner (system.in); system.out.print("enter number of cookies you'd create "); int number = input.nextint(); if (number <=0) { system.out.println(" please come in valid number") int number = input.nextint(); } input.close(); homecoming number; }
system.out.print("enter number of cookies you'd make:"); int number = input.nextint(); while(number<=0) //as long number 0 or less, repeat prompting { system.out.println("please come in valid number:"); number = input.nextint(); }
this info validation. can done do-while
loop or while
loop. can read on topics of using loops.
remarks on codes: shouldn't declare number
twice. doing int
number
more 1 time in above codes (which within same scope).
java
No comments:
Post a Comment