java - How to run a continuous loop until a value in the correct range has been entered -



java - How to run a continuous loop until a value in the correct range has been entered -

i need write java application takes number input user , validates input ensure between 1 , 10. if input not in right range application should utilize loop go on asking user input until value in right range has been entered.

here's code have far:

import java.util.scanner; class number { public static void main(string args[]) { int j = 0; int j = 0; scanner input = new scanner(system.in); system.out.println("enter number between 1 , 10: "); j = input.nextint(); while(j > 10){ j = input.nextint();//read input user system.out.println(); if (j >=1 && j <=10 ) { system.out.println("validated input: " + j); input.close(); }//end if statement else { system.out.println("enter number between 1 , 10"); j = input.nextint(); }//end else statement }//end while loop }//end main method }//end class

i can code work without while loop doesn't run continuously, have done question appears , come in number twice before question appears 1 time again , there error validation message.

so question is: "how while loop run correctly, haven't wrote correctly help me please"

i'm using eclipse

[...] utilize loop go on asking user input until value in right range has been entered.

while(j < 1 || j > 10)

that's pretty much it. print instruction withih loop only. there's no need have same line twice. , rid of if/else. loop checking condition. leave it, status fulfilled.

java eclipse input while-loop

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -