代码如下:
System.out.print("输入一个字符:");
String str = input.next();
boolean isNumber = str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
if (isNumber) {
System.out.println("输入的数字是:" + str);
} else {
System.out.println("输入的ASCII码是:" + (int) str.charAt(0));
}
运行结果是:
char c= new Scanner(System.in).next().charAt(0);
if("0123456789".contains(c+"")){
System.out.println(c);
}else{
System.out.println((int)c);
}