JAVA学习(小白向)—Switch语句—2021.5.24

本文详细介绍了Java新手如何使用Switch语句,通过实例演示如何根据分数判断等级,并处理边界和默认情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA学习(小白向)—Switch语句—2021.5.24

package a5;

public class SwitchStatement {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		scoreToLevelTest();
	}// Of main
	
	/**
	 ******************************
	 * Score to level
	 * @param paraScore form 0 to 100 参数范围
	 * @return the level form A to F 返回值
	 ******************************
	 */
	private static char scoreToLevel(int paraScore) {
		// TODO Auto-generated method stub
		// E stands for error, and F stands for fail.
		char resultLevel = 'E';
		
		int tempDigitalLevel = paraScore / 10;
		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 + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 95;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 85;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 75;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 65;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 10000;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
			
	tempScore = 0;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");	
	
	tempScore = 23;
	System.out.println("Score " + tempScore + " is " + scoreToLevel(tempScore) + " Level.");
	}// Of scoreToLeveLTest
}// Of class SwitchStatement

运行结果

Score 100 is A Level.
Score 95 is A Level.
Score 85 is B Level.
Score 75 is C Level.
Score 65 is D Level.
Score 10000 is E Level.
Score 0 is F Level.
Score 23 is F Level.

学习了switch case 语句
switch case 语句语法格式如下:

switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}

Java里的void:
按照在方法申明的语法可以看出方法的定义必须有返回值,需要写出方法返回值的类型,当方法定义时用void修饰时,表示没有返回值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值