一、 if选择结构
1.1 语法
代码块01
if (条件){
代码块02
}
代码块03
执行流程:
1、执行代码块01
2、执行条件判断
如果条件是true,执行代码块02
如果条件是false,条件if
3、执行代码块03
1.2 入门案例
package com.shine.if0;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的考试成绩:");
int score = sc.nextInt();
if (score > 90) {
System.out.println("随机奖励");
}
System.out.println("OVER");
}
}
二、if-else结构
2.1 语法
代码块01
if(条件){
代码块02
} else{
代码块03
}
代码块04
执行流程:
1、执行代码块01
2、执行条件判断
如果条件是true,执行代码块02
如果条件是false,执行代码块03
3、执行代码块04
2.2 案例
案例01--成绩合格判定
package com.shine.if0;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入考试成绩:");
int score = sc.nextInt();
if (score>= 60) {
System.out.println("成绩及格");
} else {
System.out.println("成绩不及格");
}
System.out.println("OVER");
}
}
能否构成三角形
package com.shine.if0;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一条边长:");
int a = sc.nextInt();
System.out.println("请输入第二条边长:");
int b = sc.nextInt();
System.out.println("请输入第三条边长:");
int c = sc.nextInt();
if ((a+b>c) && (a+c>b) && (b+c>a)) {
System.out.println("可以构成三角形");
} else {
System.out.println("NO,不可以构成三角形");
}
System.out.println("OVER");
}
}
三、多重if
3.1 语法
代码块00
if(条件01){
代码块01
} else if(条件02){
代码块02
} else if(条件03){
代码块03
} else if(条件04){
代码块04
} ... ...{
} else {
代码块N
}
代码块N+1
执行流程:
1、执行代码块00
2、执行if
如果条件01是true,执行代码块01,if结束
如果条件01是false,判定条件02,如果条件02是true,执行代码块02,if结束
如果条件02也是false,判定条件03,软工条件03是true,执行代码块03,if结束
... ...
如果所有条件都是false,执行代码块N
3、执行代码块N+1
3.2 入门案例
package com.shine.if0
public class Demo05 {
public static void main(String[] args) {
/**
* 键盘录入一个月份,输出这个月份所在的季节和特征
*/
int month = 5
if (month == 1) {
System.out.println("一月,冬季,大雪纷飞")
} else if(month == 2) {
System.out.println("二月,冬季,新年好")
} else if (month == 3) {
System.out.println("三月,春季,万物复苏")
} else if (month == 4) {
System.out.println("四月,春季,春暖花开")
} else if (month == 5) {
System.out.println("五月,春季,百花齐放")
} else if (month == 6) {
System.out.println("六月,夏季,高考")
} else if (month == 7) {
System.out.println("七月,夏季,暑假")
} else if (month == 8) {
System.out.println("八月,夏季,继续暑假")
} else if (month == 9) {
System.out.println("九月,秋季,秋高气爽")
} else if (month == 10) {
System.out.println("十月,秋季,中秋节")
} else if (month == 11) {
System.out.println("十一月,秋季,秋意盎然")
} else if (month == 12) {
System.out.println("十二月,冬季,万物凋零")
} else {
System.out.println("月份不存在")
}
System.out.println("OVER")
}
}
3.3 优化入门案例
package com.shine.if0;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = sc.nextInt();
if (month==1 || month==2 || month==12) {
System.out.println("冬季");
} else if (month==3 || month==4 || month==5) {
System.out.println("春季");
} else if (month>=6 && month<=8) {
System.out.println("夏季");
} else if (month>=9 && month<=11) {
System.out.println("秋季");
} else {
System.out.println("月份不存在");
}
System.out.println("OVER");
}
}
练习题
package com.shine.if0;
public class Demo07 {
public static void main(String[] args) {
int day = 3;
if (day == 1) {
System.out.println("星期一,课程语文数学");
} else if (day == 2) {
System.out.println("星期二,体育美术");
}
}
}
四、if嵌套
4.1 语法
代码块00
if(条件01){
代码块01
if(条件02){
代码块03
} else{
代码块04
}
} else{
代码块02
}
代码块05
执行流程:
1、代码块00
2、执行条件01
如果条件01是true执行代码块01,执行条件02
如果条件02是true,执行代码块03
如果条件02是false执行代码块04
如果条件01是false,执行代码块02,if结束
3、代码块05
4.2 入门案例
package com.shine.if0;
public class Demo08 {
public static void main(String[] args) {
int ticket = 1;
if (ticket>0) {
System.out.println("检票通过请经过安检");
int length = 100;
if (length>=150) {
System.out.println("您携带了管制刀具,请跟我走一趟");
} else {
System.out.println("安检通过,请排队候车");
}
} else {
System.out.println("您还尚未购买车票,请先去买票");
}
}
}
练习
package com.shine.if0;
public class Demo09 {
public static void main(String[] args) {
}
}