From e1c6c346b5ed30f20f0c5c1392d37a18813b6818 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 15 Sep 2026 15:58:19 +0800 Subject: [PATCH] =?UTF-8?q?day03--3.if=E8=AF=AD=E5=8F=A5-=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F2=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 | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/day03/src/com/inmind/if01/Demo01.java b/day03/src/com/inmind/if01/Demo01.java index 69f4b13..4846cdd 100644 --- a/day03/src/com/inmind/if01/Demo01.java +++ b/day03/src/com/inmind/if01/Demo01.java @@ -26,12 +26,58 @@ if(判断条件){ 注意:格式二,语句1或语句2肯定会执行一个,但是也永远都只会执行一个 在某种简单的逻辑之下,三元运算符可以跟if-else互换,但是在开发中if-else的使用场景更广 +-------------------------------------------------------------------- +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 Demo01 { public static void main(String[] args) { + //if格式三--多条件判断 + int x = -2; + int y; + if (x >= 3) { + y = 2 * x + 1; + } else if (x>=-1 && x<3) { + y = 2 * x; + } else { + y = 2 * x - 1; + } + + System.out.println(y); + + } + + + //if格式二 + public static void ifMethod2() { //判断2个值谁大 int a = 10; int b = 13;