4.选择结构

选择结构:
 
if单选择结构:
我们很多时候需要去判断一个东西是否可行,然后我们才会去执行,这样一个过程在程序中用if语句来表示
语法:
if(布尔表达式){
//如果表达式为true将执行的语句
}

 1 package com.duan.struct;
 2 
 3 import java.util.Scanner;
 4 
 5 public class IfDemo01 {
 6     public static void main(String[] args) {
 7 
 8         Scanner scanner = new Scanner(System.in);
 9 
10         System.out.println("请输入内容:");
11         String s = scanner.nextLine();
12 
13         //equals:判断字符串是否相等
14         if (s.equals("Hello")) {
15             System.out.println(s);
16         }
17 
18         System.out.println("End");
19         scanner.close();
20     }
21 }
22 
23 测试第一次输入一个Hello的结果: 布尔表达式成立,会执行if里面的语句
24 请输入内容:
25 Hello
26 Hello
27 End
28 
29 测试第二次不输入Hello随便输入的结果: 布尔表达式不创立,不会执行if里面的语句
30 请输入内容:
31 asdfgh
32 End
If双选择结构:
假如现在有个需求,公司要收购一个软件,成功了,给人支付100万元,失败了,自己找人开发。这样的一个需求用一个if就搞不定了,我们需要有两个判断,需要一个双选择结构,所以就有了if-else结构。
语法:
if (布尔表达式){
//如果布尔表达式的值为true
}else {
//如果布尔表达式的值为false
}
 1 package com.duan.struct;
 2 
 3 import java.util.Scanner;
 4 
 5 public class IfDemo02 {
 6     public static void main(String[] args) {
 7         //考试分数大于60分就是及格,小于60分就是不及格
 8         Scanner scanner = new Scanner(System.in);
 9 
10         System.out.println("请输入成绩:");
11         int score = scanner.nextInt();
12 
13         if (score > 60) {
14             System.out.println("及格");
15         } else {
16             System.out.println("不及格");
17         }
18 
19     }
20 }
21 
22 结果:
23 请输入成绩:
24 61
25 及格
26 
27 请输入成绩:
28 60
29 不及格
if多选择结构:
我们发现刚才的代码不符合实际的情况,真实的情况还可能存在ABCD,存在区间多级判断。比如90-100就是A,80-90就是B..等等,在生活中我们很多时候的选择也不仅仅只有两个,所以我们需要建立一个多选择结构来处理问题。
语法:
if (布尔表达式1) {
//如果表达式1成立的值为true的代码
} else if (布尔表达式2) {
//如果表达式2成立的值为true的代码
} else if (布尔表达式3) {
//如果表达式3成立的值为true的代码
} else {
//如果以上表达式都不为true执行的代码
}
if 语句之多可以有1个 else 语句,else 语句在所有的 else if 语句之后
if 语句可以有若干个 else if 语句,他们必须在 else 语句之前
一旦其中一个符合 else if 语句检测结果为 true,其他的 else if 以及 else 语句都将跳过执行。
 1 package com.duan.struct;
 2 
 3 import java.util.Scanner;
 4 
 5 public class IfDemo03 {
 6     public static void main(String[] args) {
 7 
 8         //成绩按照等级划分
 9         Scanner scanner = new Scanner(System.in);
10 
11         System.out.println("请输入成绩:");
12         int score = scanner.nextInt();
13 
14 
15         if (score == 100) {
16             System.out.println("恭喜满分");
17         } else if (score < 100 && score >= 90) {
18             System.out.println("A级");
19         } else if (score < 90 && score >= 80) {
20             System.out.println("B级");
21         } else if (score < 80 && score >= 70) {
22             System.out.println("C级");
23         } else if (score < 70 && score >= 60) {
24             System.out.println("D级");
25         } else if (score < 60 && score >= 0) {
26             System.out.println("不合格");
27         } else {
28             System.out.println("成绩不合法");
29         }
30 
31         scanner.close();
32     }
33 }
34 
35 结果测试:
36 请输入成绩:
37 50
38 不合格
39 
40 请输入成绩:
41 100
42 恭喜满分
if嵌套结构:
使用嵌套的if...else语句是合法的,也就是说你可以在另一个 if 或者else if 语句中使用 if 或者 else if 语句。你可以像 if 语句一样嵌套 else if ... else。
 
语法:
if (布尔表达式1) {
//如果布尔表达式1的值为true执行代码
if (布尔表达式2) {
//如果布尔表达式2的值为true执行代码
}
}
 


 

猜你喜欢

转载自www.cnblogs.com/duanfu/p/12222098.html