day03-if的学习
This commit is contained in:
@@ -8,9 +8,52 @@ if(判断条件){
|
|||||||
|
|
||||||
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
|
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
|
||||||
如果为true,就执行大括号内部的语句,false就直接跳过大括号中的内容
|
如果为true,就执行大括号内部的语句,false就直接跳过大括号中的内容
|
||||||
|
|
||||||
|
----------------------------------------------------------------
|
||||||
|
|
||||||
|
if格式二:
|
||||||
|
if(判断条件){
|
||||||
|
语句1;
|
||||||
|
}else{
|
||||||
|
语句2;
|
||||||
|
}
|
||||||
|
|
||||||
|
执行顺序:先执行判断条件,判断条件必须是布尔型的结果,
|
||||||
|
如果为true,就执行大括号语句1,这时else就不执行,
|
||||||
|
如果为false就直接跳过if后面大括号中的语句1直接执行else后面的语句2
|
||||||
|
|
||||||
|
注意:格式二,语句1或语句2肯定会执行一个,但是也永远都只会执行一个
|
||||||
|
|
||||||
|
在某种简单的逻辑之下,三元运算符可以跟if-else互换,但是在开发中if-else的使用场景更广
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public class Demo01_if {
|
public class Demo01_if {
|
||||||
public static void main(String[] args) {
|
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("程序结束");
|
||||||
|
|
||||||
|
//使用三元运算符来实现
|
||||||
|
String str = a>b?"a大":"b大";
|
||||||
|
System.out.println(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//if的格式一
|
||||||
|
public static void ifMethod1(String[] args) {
|
||||||
//判断下变量对应的值
|
//判断下变量对应的值
|
||||||
int i = 12;
|
int i = 12;
|
||||||
if (i!=10){
|
if (i!=10){
|
||||||
@@ -19,6 +62,17 @@ public class Demo01_if {
|
|||||||
|
|
||||||
System.out.println("程序结束");
|
System.out.println("程序结束");
|
||||||
//判断2个值谁大,请用if判断流程来判断
|
//判断2个值谁大,请用if判断流程来判断
|
||||||
|
int a = 10;
|
||||||
|
int b = 20;
|
||||||
|
|
||||||
|
//ctrl+shift+enter
|
||||||
|
if (a > b) {
|
||||||
|
System.out.println("a的值大:"+a);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b > a) {
|
||||||
|
System.out.println("b的值大:"+b);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user