public class Test4 {

//static float i=;
public static void main(String[] args){
for(int i=0;i<3;i++){
switch(i){
case 0:break;
case 1:System.out.println("one");
case 2:System.out.println("two");
case 3:System.out.println("three");
}

}
System.out.println("done");
}}
这个结果为什么是:one,two,three,two,three,done;
请帮我解释一下,谢谢!

解决方案 »

  1.   

    public class Test4 {

    //static float i=;
    public static void main(String[] args){
    for(int i=0;i<3;i++){
    switch(i){
    case 0:break;
    case 1:System.out.println("one");break;
    case 2:System.out.println("two");break;
    case 3:System.out.println("three");break;
    }

    }
    System.out.println("done");
    }}
      

  2.   

    switch语句每个case里都必须要有break;来退出switch语句
        for (int i = 0; i < 3; i++) {
          switch (i) {
            case 0:
              break;
            case 1:{
              System.out.println("one");
              break;
            }
            case 2:{
              System.out.println("two");
              break;
            }
            case 3:{
              System.out.println("three");
              break;
            }
          }    }
        System.out.println("done");
      

  3.   

            switch (i) {
              case 0:
                break;
              case 1:
                System.out.println("one");
                break;
              case 2:
                System.out.println("two");
                break;
              case 3:
                System.out.println("three");
                break;
            }
      

  4.   

    没有break,会继续往下运行。
      

  5.   

    没有BREAK语句程序会自动向下执行..
      

  6.   

    来晚了。
    就是没写break的问题
      

  7.   

    case 0:break;
    case 1:System.out.println("one");break;
    case 2:System.out.println("two");break;
    case 3:System.out.println("three");break;
    没有break,继续往下运行c
      

  8.   

    switch-case-default相当于goto所以switch的必须是整型,而case的必须是常量,因而可以在编译的时候,生成对应的label和goto。switch (x) {
      case 1: xxx(); break;
      case 2: yyy(); 
      case 3: zzz(); break;
      default: foo();
    }等价于goto labelx; //此处是x的值,比如goto label1;
    goto _default;
    label1:
    xxx(); goto end;
    label2:
    yyy();
    label3:
    zzz(); goto end;
    _default:
    foo();
    end:
      

  9.   

    注意这是细节问题你要是有语言基础的话像c  c++中就已经提到
    switch(条件)

       case 条件 :语句 ; break;//跳出整个语句块继续去判断下一个
         .
         .
         .
         .
         .