A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 一帆风顺 中级黑马   /  2012-9-27 08:19  /  1830 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王博 于 2012-9-28 08:36 编辑

class ForForDemo
{
    public static void main(String[] args)
   {
        int x=0;
        if (x>0) x=1;
       switch (x)
      {
          case 1:System.out.println(1);
          case 0:System.out.println(0);
          case 2:System.out.println(2);
          break;
          case 3:System.out.println(3);
         default:System.out.println(4);
         break;
        //输出结果是0
                            2
        //  第二个为什么是2啊,if(x>0)x=1是怎么理解的啊?
        //  求指点。。。
  }

6 个回复

倒序浏览
class ForForDemo
{
    public static void main(String[] args)
   {
        int x=0;
        if (x>0) x=1;
       switch (x)
      {
          case 1:System.out.println(1);
          case 0:System.out.println(0);
                   break;//这里少了break,switch()判断x是0后就执行case 0:了,但是你没有加break,所以直接执行case  2,但是case 2有break,所以输出2后就跳出了,所以结果是   0   2
          case 2:System.out.println(2);
          break;
          case 3:System.out.println(3);
         default:System.out.println(4);
         break;
        //输出结果是0
                            2
      
  }

回复 使用道具 举报
这里首先你要弄清switch的结束标记什么?
switch语句的结束标记是当执行到break或者switch语句末尾时,执行就结束。
楼上对你的疑问已经说明白了。这里再举一个列子来方便你理解。
public class Test
{
        public static void main(String[] args)
       {
              int x = 2;
  
             switch(x)
            {
                  case 1:System.out.print("1");
                  case 2:System.out.print("2");
                  case 3:System.out.print("3");
                  case 4:System.out.print("4");
                  default:System.out.print("no");
            }
       }
}

这里语句中就没有break,显然这里switch执行的结束标记就是switch语句结束,所以运行的结果为 2 3 4 no
回复 使用道具 举报
  int x = 0;
  if (x > 0) {
   x = 1;//这边你补全大括号,就看到 如果x>0,就把1给x。
  }
  switch (x) {//而这边x还是0
  case 1:
   System.out.println(1);
  case 0://从这开始满足,遇到break,才会退出switch语句,所以会是0和2
   System.out.println(0);
  case 2:
   System.out.println(2);
   break;
  case 3:
   System.out.println(3);
  default:
   System.out.println(4);
   break;
  }
回复 使用道具 举报
class ForForDemo
{
     public static void main(String[] args)
   {
         int x=0;
         if (x>0) x=1;
        switch (x)
       {
           case 1:System.out.println(1);
           case 0:System.out.println(0);
           case 2:System.out.println(2);
           break;
           case 3:System.out.println(3);
          default:System.out.println(4);
          break;
           }

}//x本来就是等于0的,x=0之后,先执行case:0,但你没有返回,然后又接着执行case:2,然后再break。没什么问题啊
回复 使用道具 举报

  1. <P> </P>
复制代码
class ForForDemo
{
    public static void main(String[] args)
   {
        int x=0;//你可以这改成>0的值,那么运行结果就是:1 0 2
        if (x>0)
               x=1;
       switch (x)
      {
          case 1:System.out.println(1);
          case 0:System.out.println(0);//因为case:0后面没有break语句,所以程序还会向下执行,遇到break语句才会结束
          break;
          case 2:System.out.println(2);
          break;
          case 3:System.out.println(3);
         default:System.out.println(4);
         break;
}

回复 使用道具 举报
刘学 中级黑马 2012-9-27 10:58:20
7#
本帖最后由 刘学 于 2012-9-27 11:00 编辑

class ForForDemo
{
    public static void main(String[] args)
   {
        int x=0;
        if (x>0) x=1;   
/* if (x>0) x=1;   省略了一个大括号,
相当于 if (x>0)
               {
                    x=1;  
               }  
当if语句或循环语句当大括号中只有一句话时,可以将大括号省略。
*/
       switch (x)    //x=0所以从 case:0开始执行
      {
          case 1:System.out.println(1);
          case 0:System.out.println(0);    //因为x等于0,从这句开始执行,因为他后面没有break;所以从他一下的语句都会执行直到碰到break;跳出switch,所以结果为0  2
          case 2:System.out.println(2);
          break;
          case 3:System.out.println(3);
         default:System.out.println(4);
         break;
        //输出结果是0
                            2
        //  第二个为什么是2啊,if(x>0)x=1是怎么理解的啊?
        //  求指点。。。
  }

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马