From ee2273a299fd990fd6ca2fb44807fb60790e4a2f Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Thu, 16 Jul 2026 13:57:53 +0800 Subject: [PATCH] =?UTF-8?q?day03-if=E8=AF=AD=E5=8F=A5-=E6=A0=BC=E5=BC=8F2?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E5=92=8C=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day03/src/com/inmind/if01/Demo01.java | 40 +++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) 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); } - - } }