java的ture||true&&false等於true?

2009-03-31 6:22 pm
我要問運算子的優先順序,
原式如下:
x=5*3<20&3+7>9-1||20>=20-30&& flase
結果算到到數第二行,結果如下:
x=ture||true&&false
照理說x的結果為false,||和&的優先權都一樣呀,
且是左聯結,就是先遇到誰,誰先算。
所以應該是true||true先算,x=(ture||true)&&false,
x=ture&&false=false
可是結果是true,那是不是代表是後面先算,
x=true||(true&&false)=true

請會的人教一下,謝謝!

回答 (2)

2009-03-31 9:52 pm
✔ 最佳答案
原式如下:
x=5*3<20&3+7>9-1||20>=20-30&& flase
=======一步一步解=======
1.*,/,%
x=(5*3)<20 & 3+7>9-1 || 20>=20-30 && flase

2.+,-
x=(5*3)<20 & (3+7)>(9-1) || 20>=(20-30) && flase

3.<<,>>,>>>
x=((5*3)<20) & ((3+7)>(9-1)) || (20>=(20-30)) && flase

4.&
x=(((5*3)<20) & ((3+7)>(9-1))) || (20>=(20-30)) && flase

5.&&
x=(((5*3)<20) & ((3+7)>(9-1))) || ((20>=(20-30)) && flase)

6.||
x=
(((5*3)<20) & ((3+7)>(9-1))) ||
((20>=(20-30)) && flase)


=======由高至低=======
()
++,--,!,-,~
*,/,%
+,-
<<,>>,>>>
==,!=

︿

&&
||
?:
=,*=,/=,%=,+=,- =,<<=,>>=,>>>=,&=,|=,^=

2009-03-31 14:09:18 補充:
<<,>>,>>>

==,!=
的中間加下行
<、<=、>、>=

2009-03-31 14:10:18 補充:
算術運算>比較運算>邏輯運算
參考: me
2009-03-31 6:52 pm
In Java, &amp;&amp; has precedence over || and thus, you end up with true || (true &amp;&amp; false).

Check out the precedence table for Java operators:
http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html


收錄日期: 2021-05-01 22:50:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090331000015KK02317

檢視 Wayback Machine 備份