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 (NumberFormatException e) {
try {
new BigInteger(line);
System.out.println("您录入的整数过大,请重新录入:");
} catch (Exception e1) {
try {
new BigDecimal(line);
System.out.println("您录入的是小数,请重新录入:");
} catch (Exception e2) {
System.out.println("您录入的是非法字符,请重新录入:");
}
}
}
}
}
|
|