.class错误无法弄明白

时间:2017-05-03 07:04:35

标签: java calculator

.class预计在第68行 返回双重结果; 该程序不会编译,但我无法弄清楚任何人都可以帮忙吗?

这是一个基本的计算器java文件tkaes用户数字并根据运营商输入的数据执行数学

// Calculator.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input:  Interactive.
// Output:  Result of arithmetic operation

import javax.swing.*;


public class Calculator
{
    public static void main(String args[]) 
    {
        double numberOne, numberTwo;                
        String numberOneString, numberTwoString;
        String operation;
        double result ;     

        numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
        numberOne = Double.parseDouble(numberOneString); 
        numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
        numberTwo = Double.parseDouble(numberTwoString); 
        operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");

        // Call performOperation method here        

        result = performoperation(numberOne, numberTwo, operation);
        System.out.format("%.2f",numberOne);
        System.out.print(" " + operation + " ");
        System.out.format("%.2f", numberTwo);
        System.out.print(" = ");
        System.out.format("%.2f", result);

        System.exit(0);

    } // End of main() method.


    // Write performOperation method here.

public static double performOperation(double numberOne, double numberTwo, String operation)

{


double result  = 0;


if (operation .equals("+")){
result = numberOne + numberTwo;
}

else if (operation .equals("-")){
result = numberOne - numberTwo;
}

else if (operation .equals("/")){
result = numberOne / numberTwo;
}

else if (operation .equals("*")){
result = numberOne * numberTwo;
}

else if (operation .equals("%")){
result = numberOne % numberTwo; 
}


return double result; 


System.out.println("The result is " + result);


}// END


} // End of Calculator class.

2 个答案:

答案 0 :(得分:3)

您的代码中存在一些错误:

  1. 您尝试返回double result,事实上,您应该只返回result

  2. 在你的main方法中,你要求的是一个不存在的方法,即'performoperation',而你的方法叫做'performOperation'。 Java区分大小写。

  3. 您在返回结果后尝试打印结果,但这不起作用。你必须在返回之前打印。

  4. 我修复了你的一些代码,并将你的if / else语句转换为switch case(在我看来,当你有重复的代码时更容易处理)。

    import javax.swing.*;
    
    public class Calculator {
    
    public static void main(String args[]) {
        String numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
        double numberOne = Double.parseDouble(numberOneString);
        String numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
        double numberTwo = Double.parseDouble(numberTwoString);
        String operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
    
        // Call performOperation method here        
        double result = performOperation(numberOne, numberTwo, operation);
        System.out.format("%.2f", numberOne);
        System.out.print(" " + operation + " ");
        System.out.format("%.2f", numberTwo);
        System.out.print(" = ");
        System.out.format("%.2f", result);
    
        System.exit(0);
    
    } // End of main() method.
    // Write performOperation method here.
    
    public static double performOperation(double numberOne, double numberTwo, String operation) {
        double result = 0;
    
        switch (operation) {
            case "+":
                result = numberOne + numberTwo;
                break;
            case "-":
                result = numberOne - numberTwo;
                break;
            case "/":
                result = numberOne / numberTwo;
                break;
            case "*":
                result = numberOne * numberTwo;
                break;
            case "%":
                result = numberOne % numberTwo;
                break;
            default:
                break;
        }
    
        System.out.println("The result is " + result);
        return result;
    
    }// END
    } // End of Calculator class.
    

答案 1 :(得分:0)

您有多个错误:

。错误: 这是不正确的,因为方法的名称是performOperation:

result = performoperation(numberOne, numberTwo, operation);

所以你必须把它改成:

result = performOperation(numberOne, numberTwo, operation);

2。错误:这里的语法不正确,如果您考虑一下,在 return 语句之后使用代码是没有意义的,因为该方法将返回它具有的任何值:

return double result; 

System.out.println("The result is " + result);

所以改为:

System.out.println("The result is " + result);

return result;

值得一提的两件事:

  1. 例如,当您将变量与String进行比较时,最好这样做:

    “+”。等于(操作)

  2. 这样你可以避免 NullPointerException (想想如果你的代码由于某种原因得到 null 作为操作会发生什么?它会将null与“ +“字符串,你会得到 NullPointerException )。您可以查看 Enum 的概念,以更安全的方式处理操作;)

    1. 应用程序将在main方法结束时终止,因此Java中不需要 System.exit(0)