代码:
if (this.price<temp.price)
return 1;
else if (this.price>temp.price)
return -1;
return 0;这最后一行的return 0;是属于第一个if还是属于第二个if呢或者它不属于任何if,
如何判断if语句的语句体到底包含到那些语句啊!
if (this.price<temp.price)
return 1;
else if (this.price>temp.price)
return -1;
return 0;这最后一行的return 0;是属于第一个if还是属于第二个if呢或者它不属于任何if,
如何判断if语句的语句体到底包含到那些语句啊!
解决方案 »
- java 怎么去生成短格式的XML文件<Allow value="中文"></Allow >改成<Allow value="中文"/>
- SCJ泛型题目 求解P
- 有一个字符数组,把它用了toString()方法以后,能否恢复为原来的字符数组
- 如何往swing的JTable中动态生成值,高分+追加,急~~~~~~~~~~~~
- java 如何在窗体最小化时实现状态栏闪动效果?就像某一个窗体触发某一事件,或就像QQ接收到对方发送的消息时,状态栏的最小化图标闪动的效果
- 一个牛人的博客,高手们进来看看。
- 图片文件要放在什么位置才能在JPanel上显示图片呢??
- 哪个网站可以Down电脑图书
- 新手上路:编译问题,.JAVA文件能在SUNONE里面编译,但不能在DOS下编译,why?
- sybase的JDBC驱动字符串是什么阿??
- 大家来看下我的程序为什么会出错的-_-
- 20分~求Think in java 第3版习题答案~!
if如果没有大括号的话,范围就是下一条语句。
如果没有大括号,每个if语句(或者else,else if语句)只包含它下面的一个语句
你所说的return 0;就不被上面的语句所包含咯
if (this.price<temp.price)
return 1;
else if (this.price>temp.price)
return -1;
else return 0;
return 1;
else if (this.price>temp.price)
return -1;
return 0;
等价 于这... 任何if不属于!
你的意思是下面那一种啊
if ()
{}
else
{}
else
{}还是这样理解
if ()
{}
else
{if ()
else
}
你的意思是下面那一种啊
if ()
{}
else
{}
else
{}这个有问题哦,一个if不能有两个else,但是可以有多个else if
if (this.price<temp.price) {
return 1;
} else if (this.price>temp.price) {
return -1;
}
return 0;
前面不满足才返回0啊
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 循环 ——让从更易明白!)累死了,好像还没有说全呢。看书找书看吧,不打了……
{
return 1;
}
else
{
if (this.price>temp.price)
{
return -1;
}
else { return 0;}
}
是这样。没有什么elseif ;
if (this.price>temp.price)
return -1;
else return 0;
也是一个语句,所以可以不加。在比较简短的一个语句一般为了省事把它省略!楼主好好看书先呀!!
你是说:if (this.price<temp.price)
return 1;
else
{
if (this.price>temp.price)
return -1;
else return 0;
}
这样的意思吗
if (this.price<temp.price)
{
return 1;
}
else if (this.price>temp.price)
{
return -1;
}
else
{
return 0;
}
if如果没有大括号的话,范围就是下一条语句。
=====================================
对
if (this.price<temp.price)
return 1;
else if (this.price>temp.price)
return -1;这里已经结束了。。
但是这个程序仍然可以达到最初的目的
因为只有三种情况,如果前两种没有return的话,就只有相等的情况了。。
其实可以写成:
if (this.price<temp.price) return 1;
if (this.price>temp.price) return -1;
return 0;
还可以写成这样呢:return (int)java.lang.Math.signum(temp.price-this.price);