代码:
if (this.price<temp.price)
 return 1;
else if (this.price>temp.price)
 return -1;
return 0;这最后一行的return 0;是属于第一个if还是属于第二个if呢或者它不属于任何if,
如何判断if语句的语句体到底包含到那些语句啊!

解决方案 »

  1.   

    不属于任何一个if里。
    if如果没有大括号的话,范围就是下一条语句。
      

  2.   

    如果有大括号,那么所有大括号里面的语句都被包括在上一个if语句
    如果没有大括号,每个if语句(或者else,else if语句)只包含它下面的一个语句
    你所说的return 0;就不被上面的语句所包含咯
      

  3.   

    这里,作用等价于:
    if (this.price<temp.price)
     return 1;
    else if (this.price>temp.price)
     return -1;
    else return 0;
      

  4.   

    if (this.price<temp.price)
     return 1;
    else if (this.price>temp.price)
     return -1;
    return 0;
    等价 于这...  任何if不属于!
      

  5.   

    to:malligator(大螟)
    你的意思是下面那一种啊
    if ()
    {}
    else
    {}
    else
    {}还是这样理解
    if ()
    {}
    else
    {if ()
     else
    }
      

  6.   

    to:malligator(大螟)
    你的意思是下面那一种啊
    if ()
    {}
    else
    {}
    else
    {}这个有问题哦,一个if不能有两个else,但是可以有多个else if
      

  7.   

    这里,作用等价于:
    if (this.price<temp.price) {
     return 1;
    } else if (this.price>temp.price) {
     return -1;
    }
    return 0;
      

  8.   

    lz理解return吗?
    前面不满足才返回0啊
      

  9.   

    if (this.price<temp.price)
     return 1;
    else if (this.price>temp.price)
     return -1;
    return 0;  //如果以上两个条件都不满足,返回0.就是说价格既不大于temp.price也不小于temp.price,等价于价格等于temp.price,恰恰是排除以上两种情况的情形.所以我说:这里,作用等价于:
    if (this.price<temp.price)
     return 1;
    else if (this.price>temp.price)
     return -1;
    else return 0;普遍的说,应该是:
     buyaowen(外包需要掌握三点) ( ) 信誉:100    Blog  2006-11-24 11:17:35  得分: 0     
    这里,作用等价于:
    if (this.price<temp.price) {
     return 1;
    } else if (this.price>temp.price) {
     return -1;
    }
    return 0; zzmdegm(剑心) ( ) 信誉:100    Blog  2006-11-24 11:20:31  得分: 0  
     
     
       大家的意思是这段代码不管如何返回的就是“0”,对吗
      
    ===========================
    不对.是在上边没有返回的情况下才返回 0 .这里具体是只有在this.price等于temp.price才返回 0.关于if else :
    一个if 最多只能对应一个else(也可以没有).
    在没有括号时,else 匹配上面最近的if;
    如果有括号时,在括号里匹配.if/else 的分支语句是后面的一条语句或一个语句块(有大括号时).
    下面这个匹配的是A
    if(j>2)
    {//begin A
      {//begin B
       .....  } //end  B
    .........
    }//end A
    esle b=0;下面是有错误的:因为C语句隔断了,else找不到匹配
    if(i>2)
      b=2;
      c=3; //C
    else b=0;一般来说,加上个括号是最好的,即使只有一条语句(特别是这个语句是一个if else结构 或 for循环 或 while 循环 或 do..while 循环 ——让从更易明白!)累死了,好像还没有说全呢。看书找书看吧,不打了……  
     
      

  10.   

    if (this.price<temp.price)
    {
       return 1;
    }
    else 
    {
        if (this.price>temp.price)
        {
           return -1;
        }
        else { return 0;}
    }
    是这样。没有什么elseif ;
    if (this.price>temp.price)
           return -1;
    else  return 0;
    也是一个语句,所以可以不加。在比较简短的一个语句一般为了省事把它省略!楼主好好看书先呀!!
      

  11.   

    to:malligator(大螟)
    你是说:if (this.price<temp.price)
     return 1;
    else 

    if (this.price>temp.price)
     return -1;
    else return 0;

    这样的意思吗
      

  12.   

    <=>
    if (this.price<temp.price)
     { 
         return 1;
     }
    else if (this.price>temp.price)
     {
         return -1;
     }
    else 
     {
         return 0;
     }
      

  13.   

    不属于任何一个if里。
    if如果没有大括号的话,范围就是下一条语句。
    =====================================
      

  14.   

    不属于任何if,if语句到
    if (this.price<temp.price)
     return 1;
    else if (this.price>temp.price)
     return -1;这里已经结束了。。
    但是这个程序仍然可以达到最初的目的
    因为只有三种情况,如果前两种没有return的话,就只有相等的情况了。。
      

  15.   

    不属于任何if.
    其实可以写成:
    if (this.price<temp.price) return 1;
    if (this.price>temp.price) return -1;
    return 0;
      

  16.   


    还可以写成这样呢:return (int)java.lang.Math.signum(temp.price-this.price);