+ 1
How to make the java code restart if user enter false number?
3 Antworten
+ 1
You can try something like that (but it will not work in SL Playground):
* * *
Scanner scan = new Scanner(System.in);
int n = 0;
while (true) {
try {
System.out.print("Enter a number [1..1000]: ");
n = Integer.parseInt(scan.nextLine());
// Break if 'n' is in range 1..1000
if (n > 0 && n < 1001) {
break;
}
} catch (Exception e) {
System.out.println("(!) Wrong input...\n");
}
}
System.out.println("The number is: " + n);
* * *
+ 3
You could:
* Use a loop that continues if the input is false
or
* re-call the method if the input is false
0
do you have an example?
As java restarts after your program has finished with an output.