Unable to get behaviour of Switch case in java(无法在 Java 中获取 Switch 案例的行为)
问题描述
我用java 6写过小代码
公共类TestSwitch{公共静态无效主(字符串...参数){整数a = 1;System.out.println(开始");开关(一){情况1:{System.out.println(1);案例3:System.out.println(3);案例4:System.out.println(4);}案例2:{System.out.println(2);案例5:System.out.println(5);案例7:System.out.println(7);}}System.out.println(结束");}}
<块引用>
输出: start 1 2 end
我的编辑器正在显示案例 3"和案例 5"的孤立案例.它仍在运行并显示输出.
Java 中是否有类似概念的 Nastated 案例?
为什么它会给出上述输出?相反,我认为这将是开始 1 结束"
您的回复将不胜感激!
switch 替换 if else 的 but switch 语法!= if else 语法.
你忘了在每个 case 后面加上 break
.
所以下的条件不成立.
示例:
案例0:mColor.setText("#000000");休息;
您可以在文档中找到它
<块引用>break 语句是必要的,因为没有它们,switch 块中的语句就会失败:匹配 case 标签之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句.
public static void main(String...args){整数a = 1;System.out.println("开始");开关(一){情况1:System.out.println(1);休息;案例2:System.out.println(2);休息;案例3:System.out.println(3);休息;案例4:System.out.println(4);休息;案例5:System.out.println(5);休息;案例7:System.out.println(7);休息;默认:System.out.println("什么都没有");}
I have written small code in java 6
public class TestSwitch{
public static void main(String... args){
int a = 1;
System.out.println("start");
switch(a){
case 1:{
System.out.println(1);
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
case 2:{
System.out.println(2);
case 5:
System.out.println(5);
case 7:
System.out.println(7);
}
}
System.out.println("end");
}
}
Output: start 1 2 end
My editor is showing orphaned case for 'case 3' and 'case 5'.Still it is running and showing output.
Does Nastated cases like concept is there in Java?
And Why it is giving above output? rather i thought it will be 'start 1 end'
Your response will be greatly appreciated!!
Switch replaces if else's but switch syntax != If else syntax.
You forgot to put break
after each case.
So conditions under fall through.
Example:
case 0:
mColor.setText("#000000");
break;
You can find that in docs
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
public static void main(String... args){
int a = 1;
System.out.println("start");
switch(a){
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
break;
case 4:
System.out.println(4);
break;
case 5:
System.out.println(5);
break;
case 7:
System.out.println(7);
break;
default:
System.out.println("nothing");
}
这篇关于无法在 Java 中获取 Switch 案例的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在 Java 中获取 Switch 案例的行为


- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01