DAY5 基本switch 语句

1.程序入口点

定义一个scoreToLevel,接受一个整数参数paraScore返回类型是char,会返回一个单一的字符值,不在预期情况下则返回E(error)

```java
package basic;

public static char scoreToLevel(int paraScore) {
        // E stands for error, and F stands for fail.
        char resultLevel = 'E';

将paraScore除以 10,然后将结果存储在tempDigitalLevel中

```java
package basic;

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

2.switch语句

switch语句用于根据某个变量的值执行不同的代码块。这里在tempDigitalLevel的值决定接下来的执行路径,分别返回A、B、C、D、F(fail)五个等级

defalut处理:如果tempDigitalLevel的值不在预期范围0到10之内(例如负数或其他意外值),就会进入defalut,返回值为'E',表示错误状态。

switch语句整体逻辑与c语言很像易于理解,但是要注意Java的规范写法。

```java
package basic;

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';

3.输出

根据不同tempScore值输出不同等级

	```java
    package basic;

    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));

4.完整程序及输出

完整程序

```java
package basic;

/**
 * This is the fifth code. Names and comments should follow my style strictly.
 * 
 * @author Song 2378922583@qq.com.
 */
public class SwitchStatement {
    
    /**
     * 
     * The entance 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

输出

Score 100 to level is: A
Score 91 to level is: A
Score 82 to level is: B
Score 75 to level is: C
Score 66 to level is: D
Score 52 to level is: F
Score 8 to level is: F
Score 120 to level is: E


...Program finished with exit code 0
Press ENTER to exit console.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值