第20题:把只含数字的字符串转换为整数

欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/45724461
github:https://github.com/frank-cq/MyTest

第20题:输入一个表示整数的字符串,把该字符串转换成整数并输出。*例如输入字符串“345”,则输出整数345。


代码

package test020;

/**
 * Created by cq on 2015/5/14.
 * 第20题:输入一个表示整数的字符串,把该字符串转换成整数并输出。
 *        例如输入字符串“345”,则输出整数345。
 */
public class Test020 {
    //假设num表示的数在int的取值范围内
    public static int string2Int(String num){
        int nu = 0;
        for (int i=0; i<num.length(); i++){
            if (num.charAt(i) < '0' || num.charAt(i) > '9'){
                System.out.println("字符串中含有非数字字符。");
                break;
            }
            nu = nu*10 + (num.charAt(i)-'0');
        }
        return nu;
    }
    public static void main(String[] args){
        System.out.println(string2Int("123") == 123);
        System.out.println(string2Int("4291") == 4291);
    }
}




执行结果

Connected to the target VM, address: '127.0.0.1:17658', transport: 'socket'
true
true
Disconnected from the target VM, address: '127.0.0.1:17658', transport: 'socket'

Process finished with exit code 0
原文地址:https://www.cnblogs.com/read-the-spring-and-autumn-annals-in-night/p/12041976.html