我是靠谱客的博主 老迟到期待,这篇文章主要介绍switch一道面试题,现在分享给大家,希望可以做个参考。

public class Test{
	public static void main(String[] args){
		int x=4;
		switch(x){
			case 1:System.out.println(x);
			case 2:System.out.println(x);
			case 3:System.out.println(x);
			case 4:System.out.println(x);
			case 5:System.out.println(x);
			default:System.out.println(x);
		}
		
	}
}
答案:
4
4
4

在使用switch时,需要注意另外一个问题:一般必须在case语句结尾添加break语句。 否则,会执行这个case之后的其他case语句而不管case是否匹配,只到switch结束或者遇到break为止。如果在switch中省略了break语句,那么匹配的case值后的所有情况(包括default情况)都会被执行。

最后

以上就是老迟到期待最近收集整理的关于switch一道面试题的全部内容,更多相关switch一道面试题内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部