我是個白癡:包含字符串的變量失敗==用'相同'字符串測試。爲什麼?
我的代碼是下面的代碼。我跑了代碼,並輸入4作爲我的答案。但是,該程序仍然告訴我答案是不正確的。我做錯了什麼?十分感謝你的幫助。
import java.io.*;
class class1 {
public static void main (String[] args) throws IOException {
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
System.out.println("What is the answer to 2 + 2");
String answer;
answer = input.readLine();
if (answer == "4"){
System.out.println("Correct");
} else System.out.println("Incorrect");
}
}
}
不要忘記acc ept回答:) – jeff