博客
关于我
第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
你可能感兴趣的文章
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>
nginx net::ERR_ABORTED 403 (Forbidden)
查看>>
Nginx SSL 性能调优
查看>>
Nginx SSL私有证书自签,且反代80端口
查看>>