我正在尝试使用JOptionPane创建一个简单的计算器 . 我遇到了编译错误 .
import javax.swing.JOptionPane;
public class Calculator {
public static void main ( String args[] ) {
double n1 = Double.parseDouble(JOptionPane.showInputDialog(" Enter first number: "));
double n2 = Double.parseDouble(JOptionPane.showInputDialog(" Enter second number: "));
String x = JOptionPane.showInputDialog("Enter operator: ");
double result;
if (x == "+"){
result = (n1 + n2);
JOptionPane.showMessageDialog(n1, "+", n2,"=", result);
}
else if (x == "-"){
result = (n1 - n2);
JOptionPane.showMessageDialog(n1,"-", n2,"=", result);
}
else if (x == "/"){
result = (n1 / n2);
JOptionPane.showMessageDialog(n1,"/", n2,"=", result); }
else if (n2 == 0){
JOptionPane.showMessageDialog(null, "Cannot divide by 0"); }
else if (x == "*"){
result = (n1 * n2);
JOptionPane.showMessageDialog(n1,"*", n2,"=", result); }
else if (x == "%"){
result = (n1 % n2);
JOptionPane.showMessageDialog(n1,"%", n2,"=",(n1%n2)); }
}
}
我的代码会出现这些编译错误:
找到5个错误:文件:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java [line:14]错误:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator . java:14:找不到符号符号:方法showMessageDialog(double,java.lang.String,double,java.lang.String,double)location:class javax.swing.JOptionPane文件:/ Users / gcaruso / Documents / CISS 110 /模块3 /模块4 / Calculator.java [line:19]错误:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java:19:找不到符号符号:方法showMessageDialog(double,java . lang.String,double,java.lang.String,double)location:class javax.swing.JOptionPane文件:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java [line:25]错误: / Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java:25:找不到符号符号:方法showMessageDialog(double,java.lang.String,double,java.lang.String,double)location: class javax.swing.JOptionPane文件:/ Users / gcaruso / Documents / CISS 110 / Modul e 3 / Module 4 / Calculator.java [line:32]错误:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java:32:找不到符号符号:方法showMessageDialog(double,java . lang.String,double,java.lang.String,double)location:class javax.swing.JOptionPane文件:/ Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java [line:37]错误: / Users / gcaruso / Documents / CISS 110 / Module 3 / Module 4 / Calculator.java:37:找不到符号符号:方法showMessageDialog(double,java.lang.String,double,java.lang.String,double)location: class javax.swing.JOptionPane