循环和分支在我们的日常代码中出现频率很高,无论是简单的数据处理还是复杂的业务逻辑,都会经常用到,它们看似简单,但是使用起来还是有很多隐藏的问题的,接下来我们一起学习或者复习一下:
主要包括三种:if语句、if-else语句、if-else if-else语句
if语句:
if (条件表达式) {
// 条件为真时执行的代码块
}
如果要执行的语句只有一句,可以省略“{}”,但是不建议
if-else语句:
if (条件表达式) {
// 条件为真时执行的代码块
} else {
// 条件为假时执行的代码块
}
if - else i f- else语句:
public class ConditionExample {
public static void main(String[] args) {
int score = 85; // 示例分数值
if (score >= 90) {
System.out.println("优秀:成绩为A级");
}
else if (score >= 80) {
System.out.println("良好:成绩为B级");
}
else if (score >= 70) {
System.out.println("中等:成绩为C级");
}
else if (score >= 60) {
System.out.println("及格:成绩为D级");
}
else {
System.out.println("不及格:需要补考");
}
}
}
但是如果代码中出现了多个else if,那么就需要考虑简化代码了
jdk低版本写法(java7及以下):
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("星期一");
break;
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:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("无效的日期");
break;
}
}
}
int day = 2;
switch (day) {
case 1:
case 2:
case 3:
System.out.println("工作日");
break;
case 4:
case 5:
System.out.println("快周末了");
break;
case 6:
case 7:
System.out.println("周末");
break;
default:
System.out.println("无效的日期");
break;
}
jdk高版本写法(java12及以上):
用 “ -> ” 代替了break; 并且不会导致case穿透
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1 -> System.out.println("星期一");
case 2 -> System.out.println("星期二");
case 3 -> System.out.println("星期三");
case 4 -> System.out.println("星期四");
case 5 -> System.out.println("星期五");
case 6 -> System.out.println("星期六");
case 7 -> System.out.println("星期日");
default -> System.out.println("无效的日期");
}
}
}
java14开始正式支持多标签写法:
int day = 2;
switch (day) {
case 1, 2, 3 -> System.out.println("工作日");
case 4, 5 -> System.out.println("快周末了");
case 6, 7 -> System.out.println("周末");
default -> System.out.println("无效的日期");
}
这种情况下else识别不到前面的if,会编译出错:
int b = 4;
if(b < 5)
b--;
System.out.println("hello");
else{
System.out.println("world");
}
原因是,此时我们想要 if 条件判断完执行两行语句,但是没有大括号括起来,那么计算机就会认为只有“ b-- ”是 if 需要执行的代码,而输出语句是单独插进去的按顺序执行,阻断了if和else,导致else识别不到前面的 if 。
int age = 45;
if(age > 20){
System.out.println("年轻人");
}else if(age > 40){
System.out.println("中年人");
}else if(age > 60){
System.out.println("老年人");
}
虽然答案最合理的应该是输出“中年人”,但是按顺序执行,45会先满足>20,所以最后的输出结果是“年轻人”
int month = 12;
switch(month){
case 12:
case 1:
case 2:
System.out.println("冬季");
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("输入错误");
break;
}
注意此时第一个输出语句后面没有 break;
那么结果就是:
冬季
春季
确保条件表达式逻辑正确,判断数据类型的值相等用“ == ”
eg:
if(a = 5) //错误
if(a == 5) //正确
判断对象类型的值是否相等是否相等用 .equals()
eg:
String str1 = "hello";
String str2 = new String("hello");
if (str1.equals(str2)) {
System.out.println("str1 和 str2 内容相等");
}
注意,在 java 中,String是一个类
今天的分享就到这里啦,欢迎各位在评论区补充