博客
关于我
第20题:把只含数字的字符串转换为整数
阅读量:532 次
发布时间:2019-03-08

本文共 878 字,大约阅读时间需要 2 分钟。

欢迎转载,转载请务必注明出处:

github:

第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
'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
你可能感兴趣的文章
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>