public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("录入一个整数");
while (true) {
String line = sc.nextLine();
try {
int num = Integer.parseInt(line);
System.out.println(Integer.toBinaryString(num));
break;
} catch (Exception e) {
try {
new BigInteger(line);
System.out.println("输入错误,您输入了一个过大整数,请重新输入整数:");
} catch (Exception e2) {
try{
new BigDecimal(line);
System.out.println("输入错误,您输入了一个小数,请重新输入整数:");
}catch (Exception e1) {
System.out.println("录入错误,您录入的是非法字符,请重新输入一个整数:");
}
}
}
}
} |
|