案例 一
swich....case的使用
需求分析:将不同分数段的学生成绩(0-100)分别对应不同等级(A-F)输出
过程分析:
- swich.... case....流程图(每一个case后都有break)
- swich.... case....流程图(某些case后没有break,假设case 1后没有break)
package com.day02;
/**
* This is the fifth code.
*
* @author Haitao zhang1808459537@qq.com
*/
public class Day02 {
/**
*********************
* 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 Day02
运行效果
补充:
- 若在swich语句中没有找到与之对应的case,则程序会走默认的default语句。
- 实现某些功能模块时,尽量将其单独封装到一个方法里,之后在main方法里调用即可。
案例二
for循环的使用
循环是程序设计的重要一环,其中for循环一般用于已知循环次数,while循环一般用于不知道循环次数,但是并非绝对。for循环与while循环在一定条件下可以转化,下面主要说明for循环。
- for循环的流程图:
- for循环语句:
for(语句1:循环控制变量;语句2:返回值为布尔类型的表达式/方法等;语句3:控制变量更新语句)
只需合理安排这3个语句,即可让程序按照预期进行。for循环首先会执行语句1,随机执行语句2,若语句2的最终结果若为false,则退出循环;若为true,则进入循环执行循环体,之后执行语句3。也就是说我们利用for循环自身的特性,一般将 语句1 设为初始化循环变量;语句2 设为条件判断;语句3 设为条件控制,这是一个简单的因果关系,可以帮助理解for循环,同时开发for循环某些的奇奇怪怪的用法。
简单for循环累加案列:
package com.day02;
/**
* This is the sixth code
*
* @author Haitao zhang1808459537@qq.com
*/
public class Day02_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 Day02_forstatement
运行结果:
代码分析:
addToN(int paraN)方法,参数paraN作为累加终点传入for循环的语句2,整个方法返回累加结果
静态方法forStatementTest(),调用addToN()方法,形成完整的输出格式
main()方法中调用forStatementTest()方法,增强可读性
下面是一个奇奇怪怪的for循环,仅供个人娱乐
package com.day02;
/**
* This is the sixth code
*
* @author Haitao zhang1808459537@qq.com
*/
public class Day02_forstatementV2 {
/**
*********************
* The entrance of the program.
* paraN 累加的上界
* @param args Not used now.
*********************
*/
public static int paraN=10;
public static void main(String args[]) {
System.out.println("1 add to " + paraN + " is: " + addToN(paraN));
}// Of main
/**
*********************
* Method unit test.
* adding 替代for循环中的i
*********************
*/
public static boolean forStatementTest(int adding ) {
if(adding<=paraN)return true;
else return false;
}// Of forStatementTest
/**
*********************
* Add from 1 to N.
* @return The sum.
*********************
*/
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; forStatementTest(i); i++) {
resultSum += i;
} // Of for i
return resultSum;
}// Of addToN
}// Of class Day02_forstatementV2
运行结果
这里只是把for循环的 语句2替换成了一个返回值为布尔类型的方法,在方法体里控制循环次数。