我不知道爲什麼這不起作用我沒有得到編譯錯誤,但程序總是返回else語句。我應該以其他方式定義操作還是另外調用它?Java計算器與搖擺
import javax.swing.JOptionPane;
public class Calculator1 {
public static void main(String []Args) {
String firstNum;
String operation;
String secondNum;
firstNum = JOptionPane.showInputDialog("Input a number.");
secondNum = JOptionPane.showInputDialog("Input another number.");
double num1 = Double.parseDouble(firstNum);
double num2 = Double.parseDouble(secondNum);
operation = JOptionPane.showInputDialog("Input an operation sign.");
if (operation == "x") {
System.out.println(num1 * num2);
}
if (operation == "*") {
System.out.println(num1 * num2);
}
if (operation == "/") {
System.out.println(num1/num2);
}
if (operation == ":") {
System.out.println(num1/num2);
}
if (operation == "+") {
System.out.println(num1 + num2);
}
if (operation == "-") {
System.out.println(num1 - num2);
}
else {
System.out.println("Please enter an appropriate operation sign.");
}
}}
operation.equals( 「X」) –
參見此[計算器示例](http://stackoverflow.com/a/7441804/418556)。它使用'ScriptEngine'來評估文本字段中的表達式。 –