e1086. if/else语句
The if statement encloses some code which is executed only if a condition is true. The general syntax of the if statement is:
if (condition) {
body-code
}
The if statement has two parts. The condition is a boolean expression which resolves to either true or false. The body-code is code that will be executed only if the condition is true. Here is an example of the if statement.
// Returns 0 if s is null
public int getLength(String s) {
if (s == null) {
// This line of code is execute only if s is null
return 0;
}
return s.length();
}
The if statement also has an optional else part which encloses some code that is executed only if the condition is false. Here is the general syntax of the if statement with the optional else part:
if (condition) {
body-code
} else {
else-code
}
The else-code is executed only if condition resolves to false. Here is an example of the if statement with the optional else part.
// Generates a random number and prints whether it is even or odd
if ((int)(Math.random()*100)%2 == 0) {
System.out.println("The number is even");
} else {
System.out.println("The number is odd");
}
The else-code part can also be another if statement. An if statement written this way sets up a sequence of condition's, each tested one after the other in top-down order. If one of the conditions is found to be true, the body-code for that if statement is executed. Once executed, no other conditions are tested and execution continues after the sequence. Here is an example of a sequence of if/else/if statements
// Converts the time, which is in milliseconds, into a
// more readable format with higher units
public String getTimeUnit(long time) {
String unit = "ms";
if (time >= 365*24*60*60*1000L) {
unit = "years";
time /= 365*24*60*60*1000L;
} else if (time >= 24*60*60*1000) {
unit = "days";
time /= 24*60*60*1000;
} else if (time >= 60*60*1000) {
unit = "hr";
time /= 60*60*1000;
} else if (time >= 60*1000) {
unit = "min";
time /= 60*1000;
} else if (time >= 1000) {
unit = "sec";
time /= 1000;
}
// Execution continues here if any of the conditions match
return time + " " + unit;
}
Here's some sample output:
getTimeUnit(123); // 123 ms
getTimeUnit(1234)); // 1 sec
getTimeUnit(12345)); // 12 sec
getTimeUnit(123456)); // 2 min
getTimeUnit(1234567)); // 20 min
getTimeUnit(12345678)); // 3 hr
getTimeUnit(123456789)); // 1 days
getTimeUnit(1234567890)); // 14 days
getTimeUnit(12345678900L)); // 142 days
getTimeUnit(123456789000L)); // 3 years
| Related Examples |
e1086. if/else语句的更多相关文章
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- whdxlib
1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...
- 【.net 深呼吸】细说CodeDom(2):表达式、语句
在上一篇文章中,老周厚着脸皮给大伙介绍了代码文档的基本结构,以及一些代码对象与CodeDom类型的对应关系. 在评论中老周看到有朋友提到了 Emit,那老周就顺便提一下.严格上说,Emit并不是针对代 ...
- 将表里的数据批量生成INSERT语句的存储过程 增强版
将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...
- mysql学习之 sql语句的技巧及优化
一.sql中使用正则表达式 select name,email from user where email Regexp "@163[.,]com$"; sql语句中使用Regex ...
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少.但我 ...
- MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句
第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...
- Oracle 数据库语句大全
Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...
- MyBatis源码分析(二)语句处理器
StatementHandler 语句处理器,主要负责语句的创建.参数的设置.语句的执行.不负责结果集的处理. Statement prepare(Connection connection, Int ...
随机推荐
- spring 中 AOP 功能
1 PointCut 由 ClassFilter 和 MethodMatcher 构成,通过 ClassFilter 定位到类上,通过 MethodMatcher 定位到方法. 2 Spring 支持 ...
- MySQL 数据库 分页查询
在使用MySQL 进行数据库分页查询的时候最主要是使用LIMIT子句进行查询: 首先来看一下LIMIT: LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如果给出两 ...
- centos7 启动httpd的时候为什么显示是这样的
我输入 service httpd start显示一下内容:Redirecting to /bin/systemctl start httpd.service -------------------- ...
- 怎么从docker中copy文件到 本机
怎么从docker中copy文件到 本机 docker cp <containerId>:/file/path/within/container /host/path/target
- 【Android】3.22 示例22--LBS云检索功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 简介:介绍如何使用LBS.云检索用户自有数据. 详述: (1)LBS.云是百度地图针对LBS开发者推出的平台级 ...
- C++11 随机数
C++11带来诸多特性,random就是其一. 随机数由生成器和分布器结合产生 生成器generator:能够产生离散的等可能分布数值(需要种子,不然每次生存的随机数都一样) 分布器distribut ...
- python(21)实现多进程(1)
参考链接:http://www.cnblogs.com/kaituorensheng/p/4445418.html python多进程:multiprocessing python中的多线程其实并不是 ...
- eclipse javaee 插件安装
eclipese 精简版安装java ee插件 , 按图走 (eclipse 版本 : Indigo Service Release 1 (3.7.1)) java ee 在线安装地址: htt ...
- HttpClient和HttpURLConnection的区别
总结了网上的一些资源,主要有以下两个观点: 分析一: 在研究Volley框架的源码中,发现它在HTTP请求的使用上比较有意思,在Android 2.3及以上版本,使用的是HttpURLConnecti ...
- vim显示行号、语法高亮、自动缩进、添加下划线的设置
ubuntu默认是没有安装vim的,所以设置以前请先安装vim:sudo apt-get install vim. 然后 打开vim的配置文件:sudo vim /etc/vim/vimrc 或者 s ...