日撸 Java 三百行: DAY5 Switch语句

0.主题

今天用一个将百分制成绩转为等级制的程序来熟悉基本switch语句的使用,java中switch语句的使用和C/C++中是一样的。首先附上代码。

package basic;

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

程序的执行结果如下

 该程序中分数与等级的对应情况如下表

分数区间等级
90~100A
80~89B
70~79C
60~69D
0~59F
非法输入E

这样的形式很容易想到使用if/else结构来处理,但当分支过多时,这种结构多少显得有些笨拙,switch无疑更适合应用在此处场景。

1.switch语句

switch 语句将从第一个与选项值相匹配的 case 标签处开始执行,直到执行完剩下的所有语句,或遇到break为止。以上文程序为例

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

如果将case9之后的break语句取消,那么当匹配到9时,程序会先执行case 9后的resultLevel = 'A'; 紧接着继续执行case 8后的resultLevel = 'B'; 然后遇到break而退出,最后给出的即是错误的答案。因此,合理正确的使用break才能得到正确的程序。

第二个需要注意的是default的使用,仍以上文程序为例,成绩范围是[ 0, 100 ],当输入的成绩不在此范围内时,不能与任何case相匹配,此时执行default之后的语句,通过等级' E '给出反馈。

在实际生活中,程序或多或少会收到一些非法输入,面对这样的输入,算法应该要能够做出适当的反映或进行相应的处理,这称之为算法的健壮性,此处default的使用正是提供了这一保障。

2.其他

单元测试应当单独使用一个方法,而不是将它放入main方法中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值