java中char类型转换成int类型的两种方法

版权声明:作者: Daley Zou ( https://blog.csdn.net/DaleyZou ) 出处: https://blog.csdn.net/DaleyZou/article/details/81904626

方法一:

char ch = '9';
if (Character.isDigit(ch)){  // 判断是否是数字
    int num = Integer.parseInt(String.valueOf(ch));
    System.out.println(num);
}   

方法二:

char ch = '9';
if (Character.isDigit(ch)){  // 判断是否是数字
    int num = (int)ch - (int)('0');
    System.out.println(num);
}

猜你喜欢

转载自blog.csdn.net/DaleyZou/article/details/81904626