首先声明这段程序是一道读程序写结果的题目,我本人知道第一分支少一个break,只是对输出结果是 Value is two.
               Value is three.
而不是 Value is two.
       Value is 2  感到奇怪!!!!public class SwitchTest
{
  public static void main(String[] args)
  {
    int j = 2;
    switch ( j )
    {
      case 2:
        System.out.println("Value is two.");
      case 10:
        System.out.println("Value is three.");
        break;
      default:
        System.out.println("Value is " + j);
        break;
    }
  }
}

解决方案 »

  1.   

    程序是顺序执行的,
    case 2:
            System.out.println("Value is two.");
    //这个之后没有break,就会顺序执行以下语句:
    case 10:
            System.out.println("Value is three.");
            break;
    //遇到break后跳出switch,也就不会执行default的内容了
      

  2.   

    对SWITCH结构的执行没理解清楚,default是找不到匹配的case项就执行,遇到case项break后就跳出switch,就不会执行default的内容了
    public class SwitchTest
    {
      public static void main(String[] args)
      {
        int j = 2;
        if(j==2)
        {
           System.out.println("Value is two.");
           System.out.println("Value is three.");
        }else if(j==10)
        {
           System.out.println("Value is three.");
        }else
        {
           System.out.println("Value is " + j); 
        }
      }
    }
    处女贴^_^
      

  3.   

    switch ( j )
        {
          case 2:   //正确 执行values is two
            System.out.println("Value is two.");
          case 10:  ///因为上面没有break ; 所以这里不再判断 直接执行value is three
            System.out.println("Value is three.");
            break;   ///遇见break; 跳出switch块 所以没有执行value is 2
          default:
            System.out.println("Value is " + j);
            break;
        }
      

  4.   

    此题目是关于switch 
                case的部问题.
      

  5.   

    switch 语句还没有真正理解,呵呵~~~ 一楼说的都不错,