day03-if语句-格式2说明和使用

This commit is contained in:
2026-07-16 13:57:53 +08:00
parent eebe57bc08
commit ee2273a299

View File

@@ -8,9 +8,47 @@ if(判断条件){
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
如果为true就执行大括号内部的语句false就直接跳过大括号中的内容
---------------------------------------------------------------
if格式二
if(判断条件){
语句1
}else{
语句2
}
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
如果为true就执行大括号语句1这时else就不执行
如果为false就直接跳过if后面大括号中的语句1直接执行else后面的语句2
注意格式二语句1或语句2肯定会执行一个但是也永远都只会执行一个
在某种简单的逻辑之下三元运算符可以跟if-else互换但是在开发中if-else的使用场景更广
*/
public class Demo01 {
public static void main(String[] args) {
//判断2个值谁大
int a = 10;
int b = 20;
if (a < b) {
System.out.println("b的值大值为" + b);
} else {
System.out.println("a的值大值为" + a);
}
System.out.println("程序结束");
System.out.println("-----------------");
//使用三元运算符判断2个值谁大
String str = a > b ? "a的值大值为" + a : "b的值大值为" + b;
System.out.println(str);
}
//if-格式一
public static void ifMethod() {
//定义整数变量
int i = 10;
if (i != 10) {
@@ -29,7 +67,5 @@ public class Demo01 {
if (a < b) {
System.out.println("b的值大值为"+b);
}
}
}