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序列化与反序列化时字符集不一致问题的解决办法
今天的用PHP的时候无意的出现了用unserialize()函数转换老是返回false,我确认我的字符串是没错的,测试了很多次还是一样,没办法,启用了error_reporting(E_ALL)启用错 ...
- c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题
c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...
- C++第12周(春)项目2 - "双肩挑"教师
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 教师兼干部类](第11章习题9) ...
- python2.7 跨文件全局变量的方法
有关python实现跨文件全局变量的方法. 在使用Python编写的应用的过程中,有时会遇到多个文件之间传递同一个全局变量的情况.文件1:globalvar.py #!/usr/bin/env pyt ...
- php 读取功能分割大文件实例详解
在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...
- Xilinx之RAM使用指南
一. RAM 分类XILINX 的 RAM 可分为三种,分别是:单口 RAM,简化双口 RAM 和真双口 RAM.如下 图所示: 图1 单口 RAM 图2 简化双口 RAM A 口写入数据,B 口读数 ...
- angular学习笔记(五)-阶乘计算实例(2)
<!DOCTYPE html> <html ng-app> <head> <title>2.3.3计算阶乘实例2</title> <m ...
- error C1083: 无法打开包括文件:“pthread.h”
在AssetsManager项目上右键属性->配置->配置属性->C/C++->常规->附加包含目录->点中,倒三角,编辑,在最后引导路径到pthread.h文件夹 ...
- jquery50个代码段
1. 如何创建嵌套的过滤器 ? 1 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class ...
- 采用Filter的方法解决Servlet的编码问题
这样比你自己在Servlet代码中硬编码request.setCharacterEncoding, response.setCharacterEncoding方便多了 总之,如果你添加了这个filte ...