个人网站的建设,做网站的图片要多少像素,wordpress自定义头像插件,做我的狗在什么网站上看⭐ 作者#xff1a;小胡_不糊涂 #x1f331; 作者主页#xff1a;小胡_不糊涂的个人主页 #x1f4c0; 收录专栏#xff1a;浅谈Java #x1f496; 持续更文#xff0c;关注博主少走弯路#xff0c;谢谢大家支持 #x1f496; 顺序和选择 1. 顺序结构2. 分支结构2.1 … ⭐ 作者小胡_不糊涂 作者主页小胡_不糊涂的个人主页 收录专栏浅谈Java 持续更文关注博主少走弯路谢谢大家支持 顺序和选择 1. 顺序结构2. 分支结构2.1 if 语句2.2 switch 语句 1. 顺序结构 顺序结构比较简单就是按照代码书写的顺序一行一行执行。 例如
System.out.println(aaa);
System.out.println(bbb);
System.out.println(ccc);这段代码的运行结果就是从上至下输出
aaa
bbb
ccc如果调整代码的书写顺序, 则执行顺序也发生变化
System.out.println(aaa);
System.out.println(ccc);
System.out.println(bbb)输出
aaa
ccc
bbb2. 分支结构
2.1 if 语句
语法格式1
if(布尔表达式){// 语句
}如果布尔表达式结果为 true执行 if 中的语句否则不执行。
比如小明如果这次考试考到90分或以上给你奖励一个鸡腿。 public static void main(String[] args) {int score 92;if(score 90){System.out.println(吃个大鸡腿!!!);}} 运行结果 语法格式2
if(布尔表达式){// 语句1
}else{// 语句2
}如果布尔表达式结果为 true则执行 if 中语句否则执行 else 中语句。
比如小明如果这次考到90分以上给你奖励一个大鸡腿否则奖你一个大嘴巴子。
public static void main(String[] args) {int score 89;if(score 90){System.out.println(吃个大鸡腿!!!);}else{System.out.println(挨大嘴巴子!!!);}
}运行结果 语法格式3
if(布尔表达式1){// 语句1
}else if(布尔表达式2){// 语句2
}else{// 语句3
}表达式1成立执行语句1否则表达式2成立执行语句2否则执行语句3。
比如我们定义一个分数区间来划分学生的成绩 分数在 [90, 100] 之间的为优秀 分数在 [80, 90) 之前的为良好 分数在 [70, 80) 之间的为中等 分数在 [60, 70) 之间的为及格 分数在 [ 0, 60) 之间的为不及格 错误数据 代码实现 public static void main(String[] args) {int score88;//学生成绩//判断if(score 90){System.out.println(优秀);}else if(score 80 score 90){System.out.println(良好);}else if(score 70 score 80){System.out.println(中等);}else if(score 60 score 70){System.out.println(及格);}else if(score 0 score 60){System.out.println(不及格);}else{System.out.println(错误数据);}
}运行结果 实例1判断一个数字是奇数还是偶数 首先需要知道判断奇偶的规则。数学上我们判断奇偶数是以该数是否可以整除 2 为根据的一个数能够整除 2即计算的余数为 0当除以 2 后的余数为 0 时就说明了该数为偶数否则是奇数。 我们已经了解判断奇偶数的方法尝试写出代码
public static void main(String[] args) {int num 10;if (num % 2 0) {System.out.println(num 是偶数);} else {System.out.println(num 是奇数);}
}运行结果
实例2判断一个数字是正数负数还是零 大于 0 是正数小于 0 是负数所以这里只需要判断是大于、小于还是等于。 public static void main(String[] args) {int num 10;if (num 0) {System.out.println(正数);} else if (num 0) {System.out.println(负数);} else {System.out.println(0);}
}运行结果 实例3判断一个年份是否为闰年 普通闰年公历年份是4的倍数且不是100的倍数的为闰年(如2004年、2020年等就是闰年)。 世纪闰年公历年份是整百数的必须是400的倍数才是闰年(如1900年不是闰年2000年是闰年)。 public static void main(String[] args) {int year 2000;if (year % 100 0) {// 判定世纪闰年if (year % 400 0) {System.out.println(是闰年);} else {System.out.println(不是闰年);}} else {// 普通闰年if (year % 4 0) {System.out.println(是闰年);} else {System.out.println(不是闰年);}}
}运行结果 注
分号问题 这段代码的结果会是什么
public static void main(String[] args) {int x 20;if (x 10);{System.out.println(hehe);}
}运行结果 有人可能会好奇为什么这里 x 明明是20不等于10啊怎么还会打印 hehe 。
仔细观察就会发现if 语句后跟有一个分号这里导致分号成为了 if 语句的语句体而 { } 中的代码已经成为了和一个 if 无关的代码块所以会打印出来。 分号代表语句的结束
悬垂 else 问题 public static void main(String[] args) {int x 10;int y 10;if (x 20)if (y 10)System.out.println(aaa);elseSystem.out.println(bbb);
}运行结果 为什么什么都没有打印
首先if / else 语句中可以不加大括号当不加{}时他只控制下面的一条语句要控制多行必须加上大括号。 其次就是 else 是和最接近的 if 匹配并不是和谁对齐就与它匹配。
2.2 switch 语句
基本语法
switch(表达式){case 常量值1:{语句1;[break;]}case 常量值2:{语句2;[break;]}...default:{...;//内容都不满足时执行语句[break;]}
}执行流程 先计算表达式的值和case依次比较一旦有响应的匹配就执行该项下的语句直到遇到break时结束当表达式的值没有与所列项匹配时执行default 实例根据 day 的值输出星期
public static void main(String[] args) {int day 6;switch(day) {case 1:System.out.println(星期一);break;//遇到break停止跳出switch语句case 2:System.out.println(星期二);break;case 3:System.out.println(星期三);break;case 4:System.out.println(星期四);break;case 5:System.out.println(星期五);break;case 6:case 7:System.out.println(周末);break;default:System.out.println(输入有误);break;}
}运行结果 注
多个case后的常量值不可以重复switch的括号内只能是以下类型的表达式 基本类型byte、char、short、int不能是long、float、double、boolean类型 引用类型String常量串、枚举类型break 不要遗漏否则会失去 “多分支选择” 的效果switch 不能表达复杂的条件switch 虽然支持嵌套但是很丑一般不推荐毕竟这是一个看脸的世界~
使用错误类型
public static void main(String[] args) {double num1.0;switch(num) {case 1.0:System.out.println(hehe);break;case 2.0:System.out.println(haha);break;}
}编译出错 遗漏break
public static void main(String[] args) {int day 1;switch(day) {case 1:System.out.println(星期一);// break;case 2:System.out.println(星期二);break;}
}运行结果 不能表达复杂条件
// 例如: 如果 num 的值在 10 到 20 之间, 就打印 hehe
// 这样的代码使用 if 很容易表达, 但是使用 switch 无法表示
if (num 10 num 20) {System.out.println(hehe);
} 文章转载自: http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.bchgl.cn.gov.cn.bchgl.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.wclxm.cn.gov.cn.wclxm.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.xsrnr.cn.gov.cn.xsrnr.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.dnconr.cn.gov.cn.dnconr.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.fksdd.cn.gov.cn.fksdd.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn