基础语法-判断结构if语句
2024-09-07 02:48:12
基础语法-判断结构if语句
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.单分支语句
/**
* 判断结构if单分支语句
* @author 尹正杰
*
*/
public class IfDemo1 { public static void main(String[] args) {
int x = 100; if(x >20) {
System.out.println("大于");
} //if单分支的简写形式,如果if中控制的只有一行代码,那么可以把"{}"省略掉,如下所示:(生产环境不推荐这样写,可读性太差!)
if(x>20) System.out.println("大于"); System.out.println("继续执行"); if(x < 20) {
System.out.println("小于");
} //if单分支的简写形式,如果if中控制的只有一行代码,那么可以把"{}"省略掉,如下所示:(生产环境不推荐这样写,可读性太差!)
if(x < 20)
System.out.println("小于"); System.out.println("执行完毕");
} }
二.双分支语句
/**
* 判断结构if双分支语句
* @author 尹正杰
*
*/
public class IfDemo2 { public static void main(String[] args) {
int x = 100,y = 200; if(x > y) {
System.out.println("x大于 y");
}else {
System.out.println("x小于y");
} //使用三元运算符可以改写上面的双分支语句
String res = (x > y)?"x大于 y":"x小于y"; System.out.println(res); } }
三.多分支语句
/**
* 判断结构if多分支语句
* @author 尹正杰
*
*/
public class IfDemo3 { public static void main(String[] args) {
int soure = 95; if(soure > 90) {
System.out.println("稳住,别浪!");
}else if (soure > 80) {
System.out.println("加油吧,少年~");
}else if (soure > 70) {
System.out.println("继续努力,争取下次更上一层楼");
}else if (soure > 60) {
System.out.println("你还有很大的进步空间哟,加油!");
}else {
System.out.println("再不努力就成为吊车尾啦~");
}
} }
四.嵌套if语句
/**
* 嵌套if语句
* @author 尹正杰
*
*/
public class IfDemo4 {
public static void main(String[] args) {
String Gender = "girl";
byte Age = 18; if (Gender == "girl") {
if (Age <= 18) {
System.out.println("锁门");
}
}else if (Gender == "boy") {
System.out.println("开门");
}else {
System.out.println("不开门");
}
}
}
五.判断结构
三元运算符和if...else的区别:
三元运算符是一个运算符,所以运行必须有结果,而if...else语句只能控制流程,所以不一定有结果。 某些情况下,可以把if...else语句改写成三元运算符的形式:
前提是要保证if...else执行完有具体的结果出现。 if语句特点:
每一种格式都是单条语句;
第二种格式与三元运算符的区别:三元运算符运算完要要有值出现;
条件表达式无论写成什么样子,只看最终的结构是否是true或者false。
最新文章
- http cancelled状态与ajax 超时
- ZooKeeper 配置
- Deployment failure on Tomcat 6.x. Could not copy all resources to……
- sign in和sign up区别
- 如何在其他电脑上运行VS2005编译的DEBUG版应用程序
- 使用ThreadPool代替Thread
- 你可以使用 play framework 做5件很爽的事情http://www.anool.net/?p=629
- 教学目标的表述方式──行为目标的ABCD表述法
- 慕课网-安卓工程师初养成-4-7 Java循环语句之 while
- Android传感器的使用(GravieySensor)
- 【NGROK】快速实现本地Web服务到外网的映射
- 两个byte[]拼接
- tcp/ip详解 卷1 -- 链路层
- hdf5 AttributeError: &#39;UnImplemented&#39; object has no attribute &#39;read&#39;
- (jzoj snow的追寻)线段树维护树的直径
- Spring动态切换多数据源解决方案
- tp5.0.7 修复getshell漏洞
- axios中的this指向问题
- 011 Spark应用构成结构
- Python中写一个乒乓球类的游戏
热门文章
- Android 4.1 设置默认开机动态壁纸
- ExpandableListActivity
- boost::thread demo
- Myeclipse 安装时候android adt, android sdk常见问题
- 在github上保存vscode的配置(后续重新安装vscode时,可以十分方便地从github上下载安装这个保存的配置)
- 前端学习笔记系列一:1.export default / export const
- mysql dump 完全备
- Censoring「USACO 2015 Feb」
- greenplum 数组操作
- MQTT 协议学习:002- 通信报文的构成