switch-case案例*

	*					*switch-case案例**

1、凡是可以使用switch-case的结构,都可以使用if-else。反之,不成立。
2、我们写分支结构时候,但发现既可以使用switch-case,(同时switch中表达式的取值情况不多),
又可以使用if-else时候,优先switch-case。原因:switch-case的执行效率稍高

/**

  • 例子1:将char类型a,b,c,d小写变成大写,其他输出other
    */
import java.util.Scanner;

public class TestSwitch {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String word = scan.next();
		char c = word.charAt(0);
		
		switch (c) {
		case 'a':
			System.out.println("A");
			break;
		case 'b':
			System.out.println("B");
			break;
		case 'c':
			System.out.println('C');
			break;
		case 'd':
			System.out.println("D");
			break;

		default:
			System.out.println("other");
			break;
		}
	}
}

/**

  • 例子2;学生成绩>60分的,输出“及格”,低于60的输出“不及格”
  • @author asus

*/

public class TestSwitch2 {
	public static void main(String[] args) {
		int score = 64 ;
		/*
		switch(score) {
		case 0:
			System.out.println();
			break;
		case 1:
			break;
		//.....
		case 100:
			break;	
		}*/
		/*
		if(score>=60) {
			
		}else {
			
		}*/
		switch(score /10) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			System.out.println("不及格");
			break;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			System.out.println("及格");
			break;
		}
		//更优解
		switch(score /60) {
		case 0:
			System.out.println("不及格");
			break;
		case 1:
			System.out.println("及格");
			break;
		}

	}
}
import java.util.Scanner;
/**
 *例子3: 3,4,5春6,7,8夏9,10,11秋12,1,2冬
 * @author cad
 *
 */
public class TestSwitch3 {
	public static void main(String[] args) {
		System.out.println("请输入月份:");
		int summer =(new Scanner(System.in).nextInt());
		
		switch(summer) {
		case 3:
		case 4:
		case 5:
			System.out.println("春");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("夏");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("秋");
			break;
		case 12:
		case 1:
		case 2:
			System.out.println("冬");
			break;
		
		
		}
	}
}
import java.util.Scanner;

/**
 *例子4: 编写程序,从键盘上输入2021年的月与天,要求程序输出输入的日期是2020的第几天 
 * 5 6:30+28+31+30+6
 * break 在switch-case中是可选的
 * @author asus
 *
 */
public class TestSwitch4 {
	public static void main(String[] args) {
		System.out.println("请输入2021的月份:");
		Scanner sc = new Scanner(System.in);
		int month = sc.nextInt();
		System.out.println("请输入2021的天数:");
		Scanner sc2 = new Scanner(System.in);
		int day = sc2.nextInt();
		int sumDays = 0;
		
		
		switch(month) {
		case 12:
			sumDays +=30;	
		case 11:
			sumDays +=31;	
		case 10:
			sumDays +=30;	
		case 9:
			sumDays +=31;	
		case 8:
			sumDays +=31;	
		case 7:
			sumDays +=30;	
		case 6:
			sumDays +=31;	
		case 5:
			sumDays +=30;	
		case 4:
			sumDays +=31;	
		case 3:
			sumDays +=28;			
		case 2:
			sumDays +=31;			
		case 1:
			sumDays +=day;		
			
			
		}
		System.out.println("2021年"+month+"月"+day+"日是当年的第"+sumDays+"天");
		
	}

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值