判断语句
学习语言最好的方式就是实践,每当掌握一个新功能时,就要立即将这个功能应用到实践中。
一、if 语句
1. 基本 if-else 语句
当条件成立时,执行某些语句;否则执行另一些语句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
} else {
System.out.printf("%d is small!\n", a);
System.out.printf("%d - 1 = %d\n", a, a - 1);
}
}
}
|
else
语句可以省略:
1
2
3
4
5
6
7
8
9
10
11
12
13 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5) {
System.out.printf("%d is big!\n", a);
System.out.printf("%d + 1 = %d\n", a, a + 1);
}
}
}
|
当只有一条语句时,大括号可以省略:
1
2
3
4
5
6
7
8
9
10
11
12
13 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a > 5)
System.out.printf("%d is big!\n", a);
else
System.out.printf("%d is small!\n", a);
}
}
|
练习:输入一个整数,输出这个数的绝对值。
1
2
3
4
5
6
7
8
9
10
11
12
13 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
if (x > 0)
System.out.println(x);
else
System.out.println(-x);
}
}
|
练习:输入两个整数,输出两个数中较大的那个。
1
2
3
4
5
6
7
8
9
10
11
12
13 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();
if (a > b)
System.out.println(a);
else
System.out.println(b);
}
}
|
if-else
语句内部也可以是if-else
语句。
练习:输入三个整数,输出三个数中最大的那个。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
if (a > b) {
if (a > c)
System.out.println(a);
else
System.out.println(c);
} else {
if (b > c)
System.out.println(b);
else
System.out.println(c);
}
}
}
|
2. 常用比较运算符
- 大于
>
- 小于
<
- 大于等于
>=
- 小于等于
<=
- 等于
==
- 不等于
!=
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();
if (a > b) System.out.printf("%d > %d\n", a, b);
if (a >= b) System.out.printf("%d >= %d\n", a, b);
if (a < b) System.out.printf("%d < %d\n", a, b);
if (a <= b) System.out.printf("%d <= %d\n", a, b);
if (a == b) System.out.printf("%d == %d\n", a, b);
if (a != b) System.out.printf("%d != %d\n", a, b);
}
}
|
3. if-else 连写:
输入一个 0 到 100 之间的分数,
如果大于等于 85,输出 A;
如果大于等于 70 并且小于 85,输出 B;
如果大于等于 60 并且小于 70,输出 C;
如果小于 60,输出 D;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
if (s >= 85) {
System.out.println("A");
} else if (s >= 70) {
System.out.println("B");
} else if (s >= 60) {
System.out.println("C");
} else {
System.out.println("D");
}
}
}
|
练习:
1.判断闰年。闰年有两种情况: 2.
(1) 能被 100 整除时,必须能被 400 整除;
(2) 不能被 100 整除时,被 4 整除即可。
输入一个年份,如果是闰年输出yes
,否则输出no
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if (year % 100 == 0) {
if (year % 400 == 0)
System.out.println("yes");
else
System.out.println("no");
} else {
if (year % 4 == 0)
System.out.println("yes");
else
System.out.println("no");
}
}
}
|
二、条件表达式
- 与
&&
- 或
||
- 非
!
例题:输入三个数,输出三个数中的最大值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
if (a >= b && b >= c)
System.out.println(a);
else if (b >= a && b >= c)
System.out.println(b);
else
System.out.println(c);
}
}
|
练习:用一条 if 语句,判断闰年。
1
2
3
4
5
6
7
8
9
10
11
12
13 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
System.out.println("yes");
else
System.out.println("no");
}
}
|
三、switch 语句
注意: swtich
语句中如果不加break
语句,则从上到下匹配到第一个case
后,会顺次执行后面每个case
中的语句。
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 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
String name;
switch(day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
case 4:
name = "Thursday";
break;
case 5:
name = "Friday";
break;
case 6:
name = "Saturday";
break;
case 7:
name = "Sunday";
break;
default:
name = "not valid";
}
System.out.println(name);
}
}
|