Java学习-Day3

基本switch 语句

一、关键字的用法

1. switch

同if-else一样是做为选择的switch,不同于if-else通过判断表达式的布尔值来执行之后的代码。switch是测量整数表达式(能够产生一个整数的表达式)的具体数值来做出“选择”并执行代码.

示例代码如下:

1
2
3
switch(整数表达式) {
代码
}

2. case

在switch中可以存在很多case。代码的执行是从由整数表达式确定的数值处开始执行,直到遇到break语句跳出switch代码块.

示例代码如下:

1
2
3
4
5
6
switch(整数表达式) {
case 整数:
case 整数:
case 整数:
......
}

3. break

用于阻断case之后代码的执行,正常来说switch中至少是需要一个break的.

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
switch(整数表达式) {
case 整数1:
代码1
break;
case 整数2:
代码2
break;
case 整数3:
代码3
break;
......
}

4. default

整数的值是无穷无尽的,但是需要对这部分进行处理。default正如if-else中else的作用一致,表达除去case已有整数的整数集合.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(整数表达式) {
case 整数1:
代码1
break;
case 整数2:
代码2
break;
case 整数3:
代码3
break;
default:
代码4
break;
}

注:default执行条件是整数表达式不为整数1、整数2和整数3.

二、单元测试

编写一个函数,里面有很多测试用例。这个函数调用需要被测试的函数,通过对结果的判断来确认函数的正确性。这样可以保证单独使用一个方法, main 方法里面的代码越少越好.

三、示例

题目描述

给出学生的成绩对其评级

输入

学生成绩,用整数表示.正常区间在[0,100]

示例:

1
78

输出

根据成绩不同,输出从A-F的一个大写字母

100 >= 成绩 >= 90,输出'A'

90 > 成绩 >= 80,输出'B'

80 > 成绩 >= 70,输出'C'

70 > 成绩 >= 60,输出'D'

60 > 成绩 >= 0,输出'F'

其余情况输出'E'

示例:

1
2
3
input: 78 ==> output:'C'
input: 60 ==> output:'F'
input:110 ==> output:'E'

具体代码

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package test;

/**
* This is the fifth code. Names and comments should follow my style strictly.
*
* @author Shihuai Wen wshysxcc@outlook.com.
*/
public class SwitchStatement {

/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
scoreToLevelTest();
}// Of main

/**
*********************
* Score to level.
*
* @param paraScore From 0 to 100.
* @return The level from A to F.
*********************
*/
public static char scoreToLevel(int paraScore) {
// E stands for error, and F stands for fail.
char resultLevel = 'E';

// Divide by 10, the result ranges from 0 to 10
int tempDigitalLevel = paraScore / 10;

// The use of break is important.
switch (tempDigitalLevel) {
case 10:
case 9:
resultLevel = 'A';
break;
case 8:
resultLevel = 'B';
break;
case 7:
resultLevel = 'C';
break;
case 6:
resultLevel = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
resultLevel = 'F';
break;
default:
resultLevel = 'E';
}// Of switch

return resultLevel;
}// of scoreToLevel

/**
*********************
* Method unit test.
*********************
*/
public static void scoreToLevelTest() {
int tempScore = 100;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 91;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 82;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 75;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 66;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 52;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 8;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));

tempScore = 120;
System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
}// Of scoreToLevelTest

}// Of class SwitchStatement

运行截图

四、总结

相比起if-else,switch虽然有很大的局限性,但在实际运用中可能会起到很大作用。例如在需要加入新功能的时候,大量的if-else可能会出现人为划分区间的重复,而采用switch进行选择精准执行代码。

基本for 语句

一、循环语句

循环语句是程序的核心.在程序中存在很多重复的步骤,运行时需要重复执行。

除了用for循环之外还有while和do-while(这两部分将在后面的章节提及)

for循环的使用需要注意以下三个问题:

  1. 控制变量的初始化
  2. 循环的条件
  3. 循环控制变量的更新

同时这三个问题将以三个表达式的形式表现在for循环中。

示例代码如下:

1
2
3
for(变量初始化表达式 ; 判断循环是否继续表达式 ; 循环变量更新表达式) {
需要循环的代码
}

二、算法的时间复杂度

算法的时间复杂度一般根据循环语句来计算。因为循环语句重复执行,主要的时间会在这个地方消耗。也就是循环的次数决定了整个算法的时间复杂度。若循环之中还有循环也要一并计算。

三、示例

函数 addToN(N)

求从1加到N的整数和。示意如下

1
1 + 2 + 3 + ... + N-1 + N

输入

一个整数用于表示上界

输出

一个正整数表示从1加到输入正整数的整数和


函数 addToNWithStepLength(N,Step)

求从1加到N的整数和,N为上界,步进为Step,当最后一个数超过N则舍弃。示意如下

1
2
若步进为2,则完整算式为
1 + 3 + 5 + ... + N-2 + N

输入

一个正整数用于表示上界,另一个正整数表示步进

1
10 2 ==> 上界为10,步进为2

输出

从1加到输入正整数且带有步进的整数和


完整代码

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package test;

/**
* This is the sixth code. Names and comments should follow my style strictly.
*
* @author ShiHuai Wen shihuaiwen@outlook.com.
*/
public class ForStatement {

/**
*********************
* The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String args[]) {
forStatementTest();
}// Of main

/**
*********************
* Method unit test.
*********************
*/
public static void forStatementTest() {
int tempN = 10;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));

tempN = 0;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));

int tempStepLength = 1;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));

tempStepLength = 2;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
}// Of forStatementTest

/**
*********************
* Add from 1 to N.
*
* @param paraN The given upper bound.
* @return The sum.
*********************
*/
public static int addToN(int paraN) {
int resultSum = 0;

for (int i = 1; i <= paraN; i++) {
resultSum += i;
} // Of for i

return resultSum;
}// Of addToN

/**
*********************
* Add from 1 to N with a step length.
*
* @param paraN The given upper bound.
* @param paraStepLength The given step length.
* @return The sum.
*********************
*/
public static int addToNWithStepLength(int paraN, int paraStepLength) {
int resultSum = 0;

for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
} // Of for i

return resultSum;
}// Of addToNWithStepLength

}// Of class ForStatement

运行截图

四、总结

计算机简单点描述就是一直做一件事,唯一不同的是在每次循环之后总伴随着变量的更新变化。

年年岁岁花相似,岁岁年年人不同。