0

我正在尝试使用 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

4

2 回答 2

2

我完成了它,所以整个事情都有效:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Calculator extends JPanel {
    public static void main(String args[]) {

        JFrame f = new JFrame();

        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.equals("+")) {
            result = (n1 + n2);
            JOptionPane.showMessageDialog(f, n1 + "+" + n2 + "=" + result);
            System.out.println(x);
        }

        else if (x.equals("-")) {
            result = (n1 - n2);
            JOptionPane.showMessageDialog(f, n1 + "-" + n2 + "=" + result);
            System.out.println(x);
        }

        else if (x.equals("/")) {
            if (n2 == 0) {
                JOptionPane.showMessageDialog(null, "Cannot divide by 0");
            }else{
            result = (n1 / n2);
            JOptionPane.showMessageDialog(f, n1 + "/" + n2 + "=" + result);
            System.out.println(x);
            }
        }

        else if (x.equals("*")) {
            result = (n1 * n2);
            JOptionPane.showMessageDialog(f, n1 + "*" + n2 + "=" + result);
        }

        else if (x.equals("%")) {
            result = (n1 % n2);
            JOptionPane.showMessageDialog(f, n1 + "%" + n2 + "=" + (n1 % n2));
        }
    }
}

"==" 与 ".equals" 不同 -

.equals 比较两个对象的值

== 比较它们是否是java中的同一个对象~

于 2014-10-08T16:01:21.853 回答
0

所以我认为你做错了方法 showMessageDialog :

JOptionPane.showMessageDialog(n1, "+", n2,"=", result); 

尝试这个:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Calculator extends JPanel {
    public static void main(String args[]) {

        JFrame f = new JFrame();

        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(f, n1 + "+" + n2 + "=" + result);
        }

        else if (x == "-") {
            result = (n1 - n2);
            JOptionPane.showMessageDialog(f, n1 + "-" + n2 + "=" + result);
        }

        else if (x == "/") {
            result = (n1 / n2);
            JOptionPane.showMessageDialog(f, n1 + "/" + n2 + "=" + result);
        } else if (n2 == 0) {
            JOptionPane.showMessageDialog(null, "Cannot divide by 0");
        }

        else if (x == "*") {
            result = (n1 * n2);
            JOptionPane.showMessageDialog(f, n1 + "*" + n2 + "=" + result);
        }

        else if (x == "%") {
            result = (n1 % n2);
            JOptionPane.showMessageDialog(f, n1 + "%" + n2 + "=" + (n1 % n2));
        }
    }
}
于 2014-10-08T15:56:52.473 回答