if (score >= 60)
System.out.print("及格");
>
>=
<
<=
==
!=
double r = Math.sqrt(2);
if(r *r ==2)System.out.println("sqrt(2) squared is 2");
else System.out.println("sqrt(2) squared is " + r * r);
執行後的結果為
sqrt(2) squared is 2.0000000000000004
因此,測試 abs(x-y) <= 0,使用
Math.abs(x-y) <= EPSILON * Math.max(Math.abs(x), Math.abs(y));
if ( name.equals("Harry") ) ...
if ( name.toUpperCase().equals("HARRY") ) ...
依字典的次序比較:
< s 在 t 之前
s.compareTo(t) == 0
> s 在 t 之後
注意: 字串(或物件)的比較不用 "==" 。
例題:
if (score >= 60)
System.out.print("及格");
else
System.out.print("不及格");
if (score >= 80)
System.out.print("甲");
else
if (score >= 70)
System.out.print("乙");
else
if (score >= 60)
System.out.print("丙");
else ...
例題:
例題:Tax.java
int x =5;
System.out.println(x < 10);
and &&
or ||
not !
例如,
if (tday == bday && tmonth == bmonth)
System.out.println("Happy Birthday!");
if (tmonth > bmonth || (tmonth == bmonth && tday > bday))
System.out.println("Too late to buy a gift");
boolean shipByAir = false;
if (!country.equals("USA")) shipByAir = true;
else if (state.equals("AK") || state.equals("HI"))
shipByAir = true;
if (shipByAir)
shippingCharge = 20.00;
else
shippingCharge = 5.00;