我已经看过这样的几个主题,但我不完全理解如何将其实现到我的代码中。
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");
}
}
如果整数超过1000,我会以某种方式循环回用户输入部分。
答案 0 :(得分:1)
试试这个,
public abstract class Temp {
boolean valid;
public Temp() {
String type = getType();
String extension = getFilenameExtension();
valid = true;
if("Image File".equals(type) && !".jpg".equals(extension) && !".png".equals(extension)) {
warnWrongImageFormat();
valid = false;
}
else if("Zip File".equals(type) && !".zip".equals(extension)) {
warnWrongZipFormat();
valid = false;
}
}
abstract String getType();
abstract String getFilenameExtension();
abstract void warnWrongImageFormat();
abstract void warnWrongZipFormat();
}
答案 1 :(得分:1)
只有输入的数字低于1或高于1000
时,您才可以使用while循环并取消它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");
}
}
}
}