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 ...
随机推荐
- php 仿thinkphp的sql类库
模仿thinkphp封装的类库 <?php /** * MySql操作类2015版 * 作者:咖啡兽兽 287962566@qq.com * 使用说明: * //包含文件 * inclode ' ...
- jenkins 批量修改配置文件
jenkins 批量修改配置文件 jenkin job 修改配置 修改前配置 <runPostStepsIfResult> <name>FAILURE</name&g ...
- Spring 中属性配置
1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...
- Java 异常处理的 9 个最佳实践
在 Java 中,异常处理是个很麻烦的事情.初学者觉得它很难理解,甚至是经验丰富的开发者也要花费很长时间决定异常是要处理掉和抛出. 所以很多开发团队约定一些原则处理异常.如果你是一个团队的新成员,你可 ...
- 甲骨文关闭OpenSSO后,OpenAM成为其继续者
消息来源:http://os.51cto.com/art/201003/190108.htm OpenAM主页连接: ht ...
- NEXYS 3开发板练手--USB UART(一)
接上一篇文章,今天来讲讲这个USB UART串口发送机. 我们知道,当我们的微处理器(单片机.FPGA.DSP等)要和电脑进行通信的时候一般会采用串行通信方式,而最常用的串行通信协议的物理层接口是RS ...
- [sh]uniq-sort-awk
题目:[百度搜狐面试题] 统计url出现次数 oldboy.log http://www.etiantain.org/index.html http://www.etiantain.org/1.htm ...
- HAProxy负载均衡原理及企业级实例部署haproxy集群
一 HAProxy简介 HAProxy是一种高效.可靠.免费的高可用及负载均衡解决方案,非常适合于高负载站点的七层数据请求.客户端通过HAProxy代理服务器获得站点页面,而代理服务器收到客户请求 ...
- Swift语言 简明基础 代码演示样例
开发环境: Mac.Xcode6.0 下面内容均可创建ios common line项目来測试 1.Hello World演示样例 使用xcode创建新的common line项目,查看主文件main ...
- 每日英语:He Diets, She Diets: More Weight-Loss Plans Target Men
Weight-loss companies are becoming savvier about getting men to go on a diet. savvier:更加精明 Men and w ...