diff --git a/day03/src/com/inmind/if01/Demo01.java b/day03/src/com/inmind/if01/Demo01.java index 6d17739..b775980 100644 --- a/day03/src/com/inmind/if01/Demo01.java +++ b/day03/src/com/inmind/if01/Demo01.java @@ -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); } - - } }