无法在java中获取Switch案例的行为

我在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"); } } 

输出:开始1 2结束

我的编辑器显示“案例3”和“案例5”的孤立案例 。它仍在运行并显示输出。

  • Java中存在类似概念的Nastated案例吗?
  • 为什么它提供以上输出? 而我认为这将是’开始1结束’

    非常感谢您的回复!!

Switch替换if else但是切换语法!= If else语法。

你忘了在每个案件后放假。

所以条件下降了。

例:

 case 0: mColor.setText("#000000"); break; 

你可以在docs中找到它

break语句是必需的,因为如果没有它们,switch块中的语句就会失败:匹配的case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式,直到遇到break语句。

 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"); } 
 switch(a){ case 1:{ System.out.println(1); case 3: 

你不能像这样嵌套案例。 Switch应该看起来像:

  switch(a){ case 1: System.out.println(1); break; case 3: .... 

或者像这样:

 switch(a){ case 1: System.out.println(1); switch(a) { case 3: //... break; case 5 : //... 

如果你不在案例结尾处添加break,则执行将在之后继续。 如果应该单独执行,则应在每个案例的末尾添加一个中断。

在案例2之前你有错误的结束括号。案例3,4被解释为标签而不是案例。

你的代码会给出编译错误,因为我们不能在大小写后使用大括号:确切的代码是:

 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"); } } 

并且输出将从1 3 4 2 5 7结束开始,因为你没有在每个案例后使用“break”。

正如他们在case 1:的无rest陈述case 1:执行直接跳转到case 2:并最终打印“开始1 2结束”..

你没有在case 2之前添加break语句。

请参阅此以了解有关fall through更多信息。

 Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. 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. 
 int a = 1; System.out.println("start"); switch (a) { case 1: { System.out.println(1); break; } case 3: { System.out.println(3); break; } case 4: { System.out.println(4); break; } case 2: { System.out.println(2); break; } case 5: { System.out.println(5); //no break will fall through and print 7 too } case 7: { System.out.println(7); break; } default:{ System.out.println("none"); } } 
  See if a=1 then your case 1 will work then 1 will pe printed if as we have not using break after case 1 so all cases are working in flow so output is coming like this if you want to execute only one case at one time then you have to put break after one case like switch(a){ case 1: System.out.println(1); break; case 3: System.out.println(3); break; case 4: System.out.println(4); break; 

然后它会在遇到break语句时突破switch开关