public class demo{
private static final String REGEX = "[\\d]+";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int first = getInput(scanner, 1);
int second = getInput(scanner, 2);
//输出
System.out.println(first + " " + second);
System.out.println(first > second);
System.out.println(first < second);
System.out.println(first == second);
scanner.close();
}
/**
* 获取输入
*/
private static int getInput(Scanner scanner, int i) {
while (true) {
//输入
System.out.println("请输入第" + i + "个数字:");
String firstStr = scanner.next();
//校验
if (!check(firstStr)) {
continue;
}
//转换
return Integer.parseInt(firstStr);
}
}
/**
* 校验输入
*/
private static boolean check(String str) {
if (null == str) {
System.out.println("您没有输入任何内容!");
return false;
}
if (!str.matches(REGEX)) {
System.out.println("您输入的内容不正确,请输入数字!");
return false;
}
return true;
}
}