初級 › if文の括弧と比較演算子 › パターン2
if文の括弧と比較演算子
= と == の取り違え、&& と and の混同など、条件式まわりの定番ミスを扱います。
1 public class Main { 2 public static void main(String[] args) { 3 int n = 5; 4 if (n ___ 5) { ^ 5 System.out.println("five"); 6 } 7 } 8 }
Main.java:4: error: incompatible types: int cannot be converted to boolean
空欄を埋めてビルドを通す
= は代入、== は比較です。Javaでは代入結果がbooleanにならないため、その場で弾かれます。
次の問題