Java学习day5

Day 5 基本Switch语句的实现

通过对Switch语句的学习,了解switch,case, break, default四种基本使用方法,实现对不同成绩的自动划分,将90-99划分为A,80-89划分为B,70-79划分为C,60-69划分为D,0-59划分为F,超过100的部分,通过default全部分为E。 当switch中的表达式没有匹配到值的时候,程序会执行default中的语句块。在case 5到case 0段中,由于没有break,判别后不会跳出,继续向下执行,直至遇见break,则可全部划分为F。当不满足0-10判别数时,执行default中的语句块,输出为E。

package test2;

public class test2 {
	public static void main(String[] args) {
		
		
		scoreToLevelTest();
	}
		 
	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 = 135;
		System.out.println("Score " + tempScore + " to level is: " + scoreToLevel(tempScore));
	}// Of scoreToLevelTest




	}


运行结果如下

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值