Reputation: 5
I've seen a few threads like this but I don't completely understand how I would implement it into my code.
import java.util.Scanner;
public class Test {
public static void main(String[] args)throws java.io.IOException {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a number between 1-1000: ");
int n = reader.nextInt();
if(90<=n && n<=110) System.out.println(n + " is close to 100");
if(190<=n && n<=210) System.out.println(n + " is close to 200");
if(290<=n && n<=310) System.out.println(n + " is close to 300");
if(390<=n && n<=410) System.out.println(n + " is close to 400");
if(490<=n && n<=510) System.out.println(n + " is close to 500");
if(590<=n && n<=610) System.out.println(n + " is close to 600");
if(690<=n && n<=710) System.out.println(n + " is close to 700");
if(790<=n && n<=810) System.out.println(n + " is close to 800");
if(890<=n && n<=910) System.out.println(n + " is close to 900");
if(n>1000)System.out.println("Your number is too high");
else System.out.println("Your number is not close to any 100s");
}
}
I'd like it to somehow loop back to the user input part if the integer is over 1000.
Upvotes: 0
Views: 57
Reputation: 181
You can use a while loop and break out of it only if the number entered is below 1 or above 1000
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws java.io.IOException {
Scanner reader = new Scanner(System. in );
Boolean exit = false;
while (!exit) {
System.out.println("Enter a number between 1-1000: ");
int n = reader.nextInt();
if (n < 0 || n > 1000) {
System.out.println("That number was invalid");
} else {
exit = true;
if (90 <= n && n <= 110) System.out.println(n + " is close to 100");
if (190 <= n && n <= 210) System.out.println(n + " is close to 200");
if (290 <= n && n <= 310) System.out.println(n + " is close to 300");
if (390 <= n && n <= 410) System.out.println(n + " is close to 400");
if (490 <= n && n <= 510) System.out.println(n + " is close to 500");
if (590 <= n && n <= 610) System.out.println(n + " is close to 600");
if (690 <= n && n <= 710) System.out.println(n + " is close to 700");
if (790 <= n && n <= 810) System.out.println(n + " is close to 800");
if (890 <= n && n <= 910) System.out.println(n + " is close to 900");
}
}
}
}
Upvotes: 1
Reputation: 4207
Try with this,
boolean flag;
do {
flag = false;
System.out.println("Enter a number between 1-1000: ");
int n = reader.nextInt();
if(90<=n && n<=110) System.out.println(n + " is close to 100");
if(190<=n && n<=210) System.out.println(n + " is close to 200");
if(290<=n && n<=310) System.out.println(n + " is close to 300");
if(390<=n && n<=410) System.out.println(n + " is close to 400");
if(490<=n && n<=510) System.out.println(n + " is close to 500");
if(590<=n && n<=610) System.out.println(n + " is close to 600");
if(690<=n && n<=710) System.out.println(n + " is close to 700");
if(790<=n && n<=810) System.out.println(n + " is close to 800");
if(890<=n && n<=910) System.out.println(n + " is close to 900");
if(n>1000) {
flag = true;
System.out.println("Your number is too high, please try again");
}
else if (n < 90) {
System.out.println("Your number is not close to any 100s");
}
} while(flag);
Upvotes: 1