三、判断、循环

一、判断

1. if……else if …… else

语法

:::tip

if(条件表达式){

​ 执行代码块;

} else if (条件表达式) {

​ 执行代码块;

} else {

​ 执行代码块;

}

:::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 引入 scanner
import java.util.Scanner;

public class demo01{
public static void main(String[] args) {
// 创建 scanner 对象
Scanner myScanner = new Scanner(System.in);

System.out.println("请输入年龄");
int age = myScanner.nextInt();

if ( age >= 18 && age < 60){
System.out.println("他成年了");
}else if ( age >= 60) {
System.out.println("他老了");
} else {
System.out.println("他未成年");
}

}
}

2. switch

::: tip

switch(表达式){

​ case 常量 1:

​ 语句块;

​ break;

​ case 常量 2:

​ 语句块;

​ break;

​ ……

​ default:

​ 语句块;

​ break;

}

:::

  1. switch 的表达式 对应一个值;
  2. 表达式的值与 case 常量相等,就会执行他的语句块;
  3. break: 表示退出 switch
  4. 如果所有匹配项都没有,则执行 default 语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 引入 scanner
import java.util.Scanner;

public class demo01{
public static void main(String[] args) {
// 创建 scanner 对象
Scanner myScanner = new Scanner(System.in);

System.out.println("请输入a ~ g 之间的字母");
char num = myScanner.next().charAt(0);

switch (num){
case 'a':
System.out.println("今天是星期一");
break;
case 'b':
System.out.println("今天是星期二");
break;
case 'c':
System.out.println("今天是星期三");
break;
case 'd':
System.out.println("今天是星期四");
break;
case 'e':
System.out.println("今天是星期五");
break;
case 'f':
System.out.println("今天是星期六");
break;
case 'g':
System.out.println("今天是星期天");
break;
default:
System.out.println("输入错误,请输入a ~ g 之间的字母");
break;
}
}
}
  1. 表达式的数据类型,应该与 case 后的常量类型一致,或可以自动转换成可以比较的类型
  2. 表达式的返回值必须是(byte、short、int、char、enum(枚举)、String)
  3. break 用于跳出 switch 语句;
  4. default 用于没有匹配项时执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 引入 scanner
import java.util.Scanner;

public class demo01{
public static void main(String[] args) {
// 创建 scanner 对象
Scanner myScanner = new Scanner(System.in);

System.out.println("请输入月份:");
int month = myScanner.nextInt();

switch (month){
case 3:
case 4:
case 5:
System.out.println("这是春天");
break;
case 6:
case 7:
case 8:
System.out.println("这是夏天");
break;
case 9:
case 10:
case 11:
System.out.println("这是秋天");
break;
case 12:
case 1:
case 2:
System.out.println("这是冬天");
break;
default:
System.out.println("请输入正确的月份");
break;
}
}
}

二、循环

1. for

::: tip

for (循环变量初始化;循环条件;循环变量迭代){

循环操作;

}

:::

  • print : 表示打印,不会自动换行
  • println: 表示打印,会自动换行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class demo01{
public static void main(String[] args) {
for(int i = 1; i< 10; i++){
for(int j = 1; j <= i; j++){
System.out.print(i + "*" + j + "=" + i*j + " ");
}
System.out.println();
}
}
}
public class demo01{
public static void main(String[] args) {
// 层数
for(int i = 1; i <= 5; i++){
// 控制空格
for(int k = 1; k <= 5 - i;k++){
System.out.print(" ");
}
// 控制 * 数量
for(int j = 1; j <= 2*i-1; j++){
if(j == 1 || j == 2*i-1 || i == 5){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

2. while

::: tip

循环变量初始化;

while(循环条件){

​ 循环语句;

​ 循环变量;

}

:::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class demo01{
public static void main(String[] args) {
int n1 = 1;
int n2 = 100;
while(n1 <= n2){
if(n1 % 3 == 0){
System.out.println(n1);
}
n1++;
}
}
}
public class demo01{
public static void main(String[] args) {
int count = 0;
while(true){
int num = (int)(Math.random()*100);
if(num == 97){
System.out.println("count=" + count);
break;
}else{
System.out.println(num);
count++;
}
}
}
}

3. do…while

至少会执行一次

::: tip

循环变量初始化;

​ do {

​ 循环体;

​ 循环变量;

​ } while (循环条件);

:::

1
2
3
4
5
6
7
8
9
10
11
12
13
public class demo01{
public static void main(String[] args) {
int num = 1;
int count = 0;
do {
if(num % 5 == 0 && num % 3 != 0){
count++;
}
num++;
}while(num <= 200);
System.out.println("1~200 之间能被 5 整除但不能被 3 整除的数字有:" + count + "个。");
}
}

4. break

  1. break 语句可以通过标签指明要终止的是那一层语句块;
  2. 如果没有指定标签,则默认退出最近的循环体;
  3. 实际开发中,尽量不要使用标签;
1
2
3
4
5
6
7
8
9
10
lable1:
for (int j = 0; j < 4; j++){
lable2:
for (int i = 0; i < 10; i++){
if (i == 2){
break lable1;
}
System.out.println("i=" + i);
}
}

5. continue

  1. continue 用于结束本次循环,继续执行下一次循环;
  2. continue 可通过标签指明跳过那一层循环,与 break 使用方法一致;
1
2
3
4
5
6
7
8
9
10
lable1:
for (int j = 0; j < 4; j++){
lable2:
for (int i = 0; i < 10; i++){
if (i == 2){
continue lable1;
}
System.out.println("i=" + i);
}
}

6.return

  1. return 跳出所在的方法;
  2. return 写在 main 方法中时,表示退出程序

案例

1. 100000 元,大于 5000 时通过路口收取 5%的过路费,小于等于 5000 时收取 1000 的过路费,总计通过几次路口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class demo01{
public static void main(String[] args) {
double money = 100000;
int count = 0;
while(true){
if(money > 50000){
double toll = money * 0.05;
System.out.println("缴纳过路费:" + toll);
money -= toll;
}else if(money >= 1000){
System.out.println("缴纳过路费:" + 1000);
money -= 1000;
}else{
System.out.println("剩余:" + money);
System.out.println("总共通过了" + count+ "次路口");
break;
}
count++;
}
}
}

2.判断一个数字是不是水仙花数(各个位上的数字的立方和等于他本身)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class demo01{
public static void main(String[] args) {
int n = 153;
int m1 = n / 100; // 获取百位
int m2 = n % 100 / 10; // 获取十位
int m3 = n % 10; // 获取各位

if (Math.pow(m1, 3) + Math.pow(m2, 3) + Math.pow(m3, 3) == n){
System.out.println( n + "是水仙花数");
}else{
System.out.println( n + "不是水仙花数");
}
}
}

3. 求出 1-1/2+1/3-1/4……1/100之和

1
2
3
4
5
6
7
8
9
10
11
12
13
public class demo01{
public static void main(String[] args) {
double m = 0;
for (int i = 1; i< 101; i++){
if(i%2 != 0){
m += 1.0/i;
}else{
m -= 1.0/i;
}
}
System.out.println(m);
}
}

4. 求 1+(1+2)+(1+2+3)+…+(1+2+3+…+100)之和

1
2
3
4
5
6
7
8
9
10
11
public class demo01{
public static void main(String[] args) {
int num = 0;
for(int i = 1; i< 101; i++){
for (int j = 1; j<=i; j++){
num += j;
}
}
System.out.println(num);
}
}