我正在尝试使用 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 [行:14] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator。 java:14:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/模块 3/模块 4/Calculator.java [行:19] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:19:找不到符号符号:方法 showMessageDialog(double,java. lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:25] 错误: /Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:25:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:32] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:32:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang .String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:37] 错误:/Users/gcaruso/Documents/CISS 110 /Module 3/Module 4/Calculator.java:37:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:32] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:32:找不到符号 符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/ Calculator.java [行:37] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:37:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double, java.lang.String,double) 位置:类 javax.swing.JOptionPane/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:32] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:32:找不到符号 符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/ Calculator.java [行:37] 错误:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java:37:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double, java.lang.String,double) 位置:类 javax.swing.JOptionPanedouble,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:37] 错误:/Users/gcaruso /Documents/CISS 110/Module 3/Module 4/Calculator.java:37:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing .JOptionPanedouble,java.lang.String,double) 位置:类 javax.swing.JOptionPane 文件:/Users/gcaruso/Documents/CISS 110/Module 3/Module 4/Calculator.java [行:37] 错误:/Users/gcaruso /Documents/CISS 110/Module 3/Module 4/Calculator.java:37:找不到符号符号:方法 showMessageDialog(double,java.lang.String,double,java.lang.String,double) 位置:类 javax.swing .JOptionPane