您的位置 首页 java

道哥说编程—细说java中的switch-case流程控制语句

package first;
/*
 *本类演示 Java 流程控制语句之switch-case语句 
 */public class SwitchDemo {
public static void main(String[] args) {
//switch语句由一个控制表达式和多个case标签组成
//在switch语句中只能接收byte short char 和int四种数据类型和枚举结构
//不能接受 boolean 类型(jdk7则可以允许接收String 字符串 类型)
/*
 * 语法:
 switch(表达式或者变量)
 {
 case 值1: //所有case中的值必须唯一
 {
 //代码块
 ......
  break ;
 }
 case 值2:
 {
 //代码块
 ......
 break;
 }
 ....
 default 
 {
 代码块 
 .......
 }
 }
 //先对表达式求值,再一一与case进行匹配,遇到匹配值相同的则进入执行
 //case语句里面都没有找到与之相匹配的值 那么进入default语句
 */
System.out.println("------------例子1-------------");
char day = 'C';//用switch实现根据day的值输出对应的星期几
//A--周一 B--周二 C周三 D--周四 E--周五 F--周六 G--周日 其他字符--输出错误
switch(day)
{
case 'A'://注意 是字符 不是字符串 字符用 单引号 
{
System.out.println("星期一");
break;//跳出当前switch语句
}
case 'B':
{
System.out.println("星期二");
break;
}
case 'C':
{
System.out.println("星期三");
break;
}
case 'D':
{
System.out.println("星期四");
break;
}
case 'E':
{
System.out.println("星期五");
break;
}
case 'F':
{
System.out.println("星期六");
break;
}
case 'G':
{
System.out.println("星期天");
break;
}
default:
{
System.out.println("条件有误");
}

//答案是输出星期一


}
System.out.println("------------例子2:演示没有break-------------");
//在switch语句中case里面的break语句非常重要,当没有break语句的时候
//程序会从匹配到的case语句开始一直往下执行直到遇到break语句 如果没有找到break语句那么将一直执行到switch语句结束
//例如
switch(day)
{
case 'A'://注意 是字符 不是字符串 字符用单引号
{
System.out.println("星期一");
//break;//跳出当前switch语句
}
case 'B':
{
System.out.println("星期二");
//break;
}
case 'C':
{
System.out.println("星期三");
//break;
}
case 'D':
{
System.out.println("星期四");
//break;
}
case 'E':
{
System.out.println("星期五");
//break;
}
case 'F':
{
System.out.println("星期六");
//break;
}
case 'G':
{
System.out.println("星期七");
//break;
}
default:
{
System.out.println("条件有误");
}

}
//OK 到了这个地方大家就可以看到 第二个switch语句把每个case语句都输出来了 因为程序没有读取break的影子 所以就一直找
//一直找 找到最后都没找到 (大家在看到这个地方的时候自己试着把有些加上有些去掉然后看看结果就更明显了)
}

}
 

大家对于以上案例有什么疑问可以在评论区给道哥留言或者@道哥头条好 道哥说编程 就OK啦

文章来源:智云一二三科技

文章标题:道哥说编程—细说java中的switch-case流程控制语句

文章地址:https://www.zhihuclub.com/191896.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

22条评论

  1. My wife and i have been absolutely more than happy that Emmanuel managed to finish off his studies through the entire ideas he gained from your very own blog. It’s not at all simplistic to just choose to be giving for free facts which usually the rest might have been making money from. So we realize we have you to thank for this. The most important explanations you’ve made, the simple blog menu, the friendships you aid to create – it’s got most overwhelming, and it is helping our son and us reason why that concept is awesome, which is certainly unbelievably mandatory. Thank you for everything!

  2. Spot on with this write-up, I actually think this website needs rather more consideration. I’ll most likely be again to read far more, thanks for that info.

  3. I am delighted that I discovered this web site, precisely the right info that I was searching for! .

  4. There is noticeably a bundle to find out about this. I assume you made sure good factors in options also.

  5. as soon as I found this site I went on reddit to share some of the love with them.

  6. My spouse and i got really ecstatic Chris managed to do his investigation with the precious recommendations he acquired from your weblog. It’s not at all simplistic to just find yourself offering tips and tricks which the others could have been making money from. We fully grasp we need the blog owner to be grateful to because of that. These illustrations you made, the straightforward site menu, the friendships your site help to create – it’s got mostly wonderful, and it’s making our son and our family understand the subject is amusing, which is really pressing. Thanks for all the pieces!

  7. I want forgathering utile information , this post has got me even more info! .

  8. Thanks so much for giving everyone such a spectacular possiblity to check tips from this site. It is often very great and also stuffed with amusement for me personally and my office peers to search your site minimum 3 times in 7 days to learn the latest guides you have. And of course, I’m so actually fascinated with the awesome creative concepts you give. Certain 4 ideas in this posting are without a doubt the finest we’ve ever had.

  9. It’s arduous to search out knowledgeable people on this matter, but you sound like you realize what you’re talking about! Thanks

  10. I am glad to be one of several visitants on this great site (:, regards for posting.

网站地图