【问题标题】:Cannot resolve method error when trying to reproduce the code尝试重现代码时无法解决方法错误
【发布时间】:2021-03-02 11:59:10
【问题描述】:

我知道这个问题解决已经有一段时间了 (Simple calculator in java - using boolean to ask if user wants to continue),但我想重新创建它,当我尝试运行它时,所有数学运算符(加、减、乘或除)都出现错误)。错误说:“无法解析方法'add'/'minus'/'multiply'/'divide'”

如果我添加实际代码可能会更容易:

    import javax.swing.JOptionPane;

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

        double numberOne;
        double numberTwo;
        double result;
        String number1;
        String number2;
        String input;
        boolean useCalculator = false;

        while (!useCalculator) {
            number1 = JOptionPane.showInputDialog(null, "This is a calculator\nEnter first number");
            numberOne = Double.parseDouble(number1);

            input = JOptionPane.showInputDialog(null, "Choose from the following options:\n+\n-\n*\n/");

            while (input == null || !(input.equals("+") || input.equals("*") || input.equals("/") || input.equals("-")))
                input = JOptionPane.showInputDialog(null, "Thank you! What would you like to do?\nPlease choose from the following options:\n+\n-\n*\n/");
            {
                input = JOptionPane.showInputDialog(null, "The operator" + input + "is invalid. Please choose a correct one");
            }
            number2 = JOptionPane.showInputDialog(null, "Enter second number");
            numberTwo = Double.parseDouble(number2);

            if (input != null && input.equals("+")) {
                add(numberOne, numberTwo);
                result = add(numberOne, numberTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numberOne + " " + input + " " + numberTwo + " is: " + result);
            } else if (input != null && input.equals("-")) {
                minus(numberOne, numberTwo);
                result = minus(numberOne, numberTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numberOne + " " + input + " " + numberTwo + " is: " + result);
            } else if (input != null && input.equals("*")) {
                multiply(numberOne, numberTwo);
                result = multiply(numberOne, numberTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numberOne + " " + input + " " + numberTwo + " is: " + result);
            } else if (input != null && input.equals("/")) {
                divide(numberOne, numberTwo);
                result = divide(numberOne, numberTwo);
                JOptionPane.showMessageDialog(null, "The result of " + numberOne + " " + input + " " + numberTwo + " is: " + result);
            }


        }

        System.out.println("End of program.");
    }
}

关于如何解决这个问题并在没有这些错误的情况下运行它有什么想法吗?

提前致谢:)

【问题讨论】:

  • 您尚未为add、minus、multiply 或divide 定义任何函数。
  • 我明白了,但我不知道该怎么写? ://
  • 我以某种方式设法让它运行没有任何错误,但是现在当我按下运行时,什么也没有发生,我收到这条消息 ""C:\Program Files\Java\jdk-15.0.2\bin \java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 社区版 2020.3.2\lib\idea_rt.jar=50314:C:\Program Files\JetBrains\IntelliJ IDEA 社区版 2020.3.2\bin " -Dfile.encoding=UTF-8 -classpath"

标签: symbols


【解决方案1】:

GUI 似乎在后台运行,所以当我最小化窗口时,我能够看到计算器并正常运行它。 控制台中显示的消息是关于旧 JDK 的提醒,所以现在一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多