day03-流程控制语句-判断流程-if格式三的使用和练习

This commit is contained in:
2026-01-14 16:50:18 +08:00
parent fe7df9217a
commit 3a88efe254
2 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
package com.inmind.if01;
/*
if格式三
if(判断条件1){
语句1
}else if(判断条件2){
语句2
}else if(判断条件3){
语句3
}....
else{
语句n;
}
执行顺序:先执行判断条件1判断条件必须是布尔型的结果
如果为true就执行大括号语句1这时结束了整个if语句
如果为false就直接跳过if后面大括号中的语句1继续向下判断判断条件2
如果为true就执行大括号语句2这时结束了整个if语句
如果为false就直接跳过if后面大括号中的语句2继续向下判断....
最终如果所有的判断条件都为false那么就直接执行else后面的语句n
注意:格式三,肯定会执行一个语句,但是也永远都只会执行一个语句;
*/
/*
x和y的关系满足如下
x>=3 y = 2x + 1;
-1<=x<3 y = 2x;
x<-1 y = 2x 1;
根据给定的x的值计算出y的值并输出。
*/
public class Demo03 {
public static void main(String[] args) {
//定义一个x的值
int x = -2;
int y;//此时y的值不确定先定义后赋值
//if-else if -else if - else的格式三实现多条件判断
if (x >= 3) {
y = 2 * x + 1;
} else if (x >= -1 && x < 3) {
y = 2 * x;
} else{//除去上面的2种情况剩下的都满足x<-1
y = 2 * x - 1;
}
System.out.println(y);
System.out.println("程序结束");
}
//if-格式三的使用
public static void ifMethod3() {
//定义一个x的值
int x = -2;
int y;//此时y的值不确定先定义后赋值
//if-else if -else if - else的格式三实现多条件判断
if (x >= 3) {
y = 2 * x + 1;
} else if (x >= -1 && x < 3) {
y = 2 * x;
} else{//除去上面的2种情况剩下的都满足x<-1
y = 2 * x - 1;
}
System.out.println(y);
System.out.println("程序结束");
}
}

View File

@@ -0,0 +1,11 @@
package com.inmind.if01;
/*
指定考试成绩,判断学生等级
90-100 优秀
80-89 好
70-79 良
60-69 及格
60以下 不及格
*/
public class Test04 {
}