本文共 878 字,大约阅读时间需要 2 分钟。
欢迎转载,转载请务必注明出处:
github:代码
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执行结果'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'truetrueDisconnected from the target VM, address: '127.0.0.1:17658', transport: 'socket'Process finished with exit code 0