Java基础进阶- switch case语句,case穿透现象

/*
----------------case穿透-----------------
switch case语句,case在没有遇到break的时候不会跳出语句,会在不判断下个case值的情况下,
继续向下执行,直到遇到break。
*/

import java.util.Scanner;

public class switchdemo{
	public static void main(String[] args){
		//创建对象
		Scanner sr = new Scanner(System.in);
		//接收对象
		int mouth = sr.nextInt();
		//进入判断
		doWay(mouth);
		
	}
	public static void doWay(int sm){
		switch(sm){
			case 1:
			case 2:
			case 12:
			System.out.println("冬季");
			//break;
			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("秋季");
			default:
			System.out.println("输入有无,请重新输入..");	
			
		}
	}
}

在这里插入图片描述

发布了18 篇原创文章 · 获赞 16 · 访问量 398

猜你喜欢

转载自blog.csdn.net/lierenbiji21/article/details/105320538