day03-流程控制语句-判断流程-if格式二
This commit is contained in:
40
day03/src/com/inmind/if01/Demo02.java
Normal file
40
day03/src/com/inmind/if01/Demo02.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.if01;
|
||||||
|
/*
|
||||||
|
if格式二:
|
||||||
|
if(判断条件){
|
||||||
|
语句1;
|
||||||
|
}else{
|
||||||
|
语句2;
|
||||||
|
}
|
||||||
|
|
||||||
|
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
|
||||||
|
如果为true,就执行大括号语句1,这时else就不执行,
|
||||||
|
如果为false就直接跳过if后面大括号中的语句1直接执行else后面的语句2
|
||||||
|
|
||||||
|
注意:格式二,语句1或语句2肯定会执行一个,但是也永远都只会执行一个
|
||||||
|
|
||||||
|
在某种简单的逻辑之下,三元运算符可以跟if-else互换,但是在开发中if-else的使用场景更广
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//判断给定的整数是奇数还是偶数,打印出结果(if-else 格式二 )
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//if的格式二的使用
|
||||||
|
public static void ifMethod2() {
|
||||||
|
//判断2个整数谁大
|
||||||
|
int a = 30;
|
||||||
|
int b = 20;
|
||||||
|
|
||||||
|
if(a >= b){
|
||||||
|
System.out.println("变量a的值大,为"+a);
|
||||||
|
}else{
|
||||||
|
System.out.println("变量b的值大,为"+b);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user