Java流程控制03:顺序、if选择结构

## 选择结构

+ if单选择结构
+ if双选择结构
+ if多选择结构
+ 嵌套的if结构
+ switch多选择结构

#### if单选择结构

+ **我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示**

* **语法:**

![](https://www.icode9.com/i/ll/?i=20200429153245110.png)

**if表达式是布尔型表达式,其结果为ture或false**

if语句流程图如下:

![](https://www.icode9.com/i/ll/?i=20200429153502538.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NTRE4yMzMzMzM=,size_16,color_FFFFFF,t_70)

```java
package com.kuangstudy.struct;

import java.util.Scanner;

public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("请输入内容:");
String s = scanner.nextLine();

if (s.equals("Hello")){
System.out.println(s);
}

System.out.println("END");

scanner.close();
}
}

```

---

#### if双选择结构

* **if-else语句格式如下:**

![](https://www.icode9.com/i/ll/?i=20200429154251827.png)

 

* **if-else语句流程图如下:**

![](https://www.icode9.com/i/ll/?i=20200429154336431.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NTRE4yMzMzMzM=,size_16,color_FFFFFF,t_70)

```java
package com.kuangstudy.struct;

import java.util.Scanner;

public class IfDemo02 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60分就不及格
Scanner scanner = new Scanner(System.in);

System.out.println("请输入成绩:");
int score = scanner.nextInt();

if (score>60){
System.out.println("成绩合格!");
}else {
System.out.println("成绩不合格。");
}

scanner.close();
}
}

```

### if多选择结构

* 语句流程图

![](https://www.icode9.com/i/?i=20170603153945164)

```java
package com.kuangstudy.struct;

import java.util.Scanner;

public class IfDemo03 {
public static void main(String[] args) {
//考试分数大于60就是及格,小于60就是不及格。
Scanner scanner = new Scanner(System.in);
/*
if 语句至少有1个else语句,else 语句在所有的else if 语句之后。
if 语句可以有若干个 else if 语句,它们必须在else语句之前。
一旦其中一个else if 语句检测为true ,其它的 else if以及else 语句都将绕过执行。
* */

System.out.println("请输入此次考试成绩:");

int score = scanner.nextInt();

if (score==100){
System.out.println("恭喜成绩满分!");
}else if(score<100 && score>=90){
System.out.println("成绩优秀!");
}else if(score<90 && score>=80){
System.out.println("成绩优良!");
}else if(score<80 && score>=70){
System.out.println("成绩中等!");
}else if(score<70 && score>=60){
System.out.println("成绩合格!");
}else if (score<60 && score>=0){
System.out.println("成绩不合格!");
}else {
System.out.println("成绩不合法,请输入有效成绩!");
}

scanner.close();
}
}

```

### 嵌套的if结构

* 使用嵌套的 if...else 语句是合法的,也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句,你可以像 if 语句一样嵌套 else if...else

```java
package com.kuangstudy.struct;

import java.util.Scanner;

public class IfDemo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字:");
int score = scanner.nextInt();
if (score<=50){
if (score<=2){
if (score==1){
System.out.println("恭喜才对了!");
}else{
System.out.println("猜错了!");
}
}else{
System.out.println("猜错了!");
}
}else {
System.out.println("猜错了!");
}

scanner.close();


}
}

```

 

上一篇:作业对比反思


下一篇:【练习/sklearn库基础】导入sklearn库自带的乳腺癌数据集,分别使用GaussianNB、MultinomialNb、BernouliNB、SVM及KNN5种分类器进行分类预测