java中char类型转换成int类型的两种方法
方法一:
第一种利用Integer包装类的方法Integer.parseInt
Copychar ch = '9';
if (Character.isDigit(ch)){ // 判断是否是数字
int num = Integer.parseInt(String.valueOf(ch));
System.out.println(num);
}
方法二:
第二种方法利用字符强制转化为int型时,转化为ASCII码的特点。其字符的ASCII码值减去0
的ASCII码值等于数值本身
Copychar ch = '9';
if (Character.isDigit(ch)){ // 判断是否是数字
int num = (int)ch - (int)('0');
System.out.println(num);
}