我的Java计算器代码中有一个未知错误

时间:2014-10-21 05:31:16

标签: java parsing math methods calculator

我是一个非常年轻且缺乏经验的Java程序员,试图理解我在高中计算机科学课上讨论的一些概念。

目前,我正在尝试使用Java制作计算器,但在我的代码中遇到了错误......我一直试图找出问题的原因,但我绝对卡住了。我正在尝试创建一个计算器,让用户在一行中输入一个等式,并通过解析,程序应该能够给我一个正确的答案。目前我只编程数学来计算具有两个操作数(+, - ,*,/,^)的方程,但我也希望我的程序计算具有单个操作数的方程(绝对值,sin,cos,tan)。

这是我到目前为止所做的:

package calculator;
import java.util.Scanner;

public class Calculator {

public class Maths {

    double add(double a, double b) {
        double answer = a+b;
        return answer;          
    }

    double subtract(double a, double b) {
        double answer = a-b;
        return answer;          
    }

    double multiply(double a, double b) {
        double answer = a*b;
        return answer;          
    }

    double divide(double a, double b) {
        double answer = a/b;
        return answer;          
    }

    double power(double a, double b){
        double answer =a;

        for (int x=2; x<=b; x++){
            answer *= a;
        }

        return answer;
    }

}

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Maths maths = Maths();

    double answer = 0;
    double inputA, inputB;
    char operator;
    boolean done = false;

     while (done == false) {
        System.out.print("Please enter your equation: ");

        inputA = input.nextDouble();
        operator = input.next().charAt(0);
        inputB = input.nextDouble();        

        if (operator == '+') {
            answer = maths.add(inputA, inputB);
        }

        if (operator == '-') {
            answer = maths.subtract(inputA, inputB);
        }

        if (operator == '*') {
            answer = maths.multiply(inputA, inputB);
        }

        if (operator == '/') {
            answer = maths.divide(inputA, inputB);
        }

        if (operator == '^') {
            answer = maths.power(inputA, inputB);
        }

            System.out.println(answer);             
    }       

    input.close();

  }

}

截至目前,我觉得我的代码正朝着正确的方向前进:除了存在重大错误,其原因我不确定。 public class Maths在我的主要代码块中不起作用。当我创建它时,我认为我将使用过程分解并创建一个单独的方法来进行计算,而实际的变量集合将在主类中发生。

我的IDE在主类的Maths maths = Maths();给出了错误。

如果我能帮助找出并理解手头问题的根源,我真的很感激,所以我可以修复我的程序并避免将来出现这样的错误!

提前感谢

2 个答案:

答案 0 :(得分:3)

你错过了new这里

Maths maths = Maths();

应该是

Maths maths = new Maths();

这个Maths是一个内部类。因此,您需要更改实例化,如下所示,以纠正所有错误。

Maths maths =new Calculator().new Maths();

答案 1 :(得分:2)

使用此代码可以使用

package calculator;
import java.util.Scanner;

class Maths {

    double add(double a, double b) {
        double answer = a+b;
        return answer;          
    }

    double subtract(double a, double b) {
        double answer = a-b;
        return answer;          
    }

    double multiply(double a, double b) {
        double answer = a*b;
        return answer;          
    }

    double divide(double a, double b) {
        double answer = a/b;
        return answer;          
    }

    double power(double a, double b){
        double answer =a;

        for (int x=2; x<=b; x++){
            answer *= a;
        }

        return answer;
    }

}

public class Calculator {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Maths maths = new Maths();

    double answer = 0;
    double inputA, inputB;
    char operator;
    boolean done = false;

     while (done == false) {
        System.out.print("Please enter your equation: ");

        inputA = input.nextDouble();
        operator = input.next().charAt(0);
        inputB = input.nextDouble();        

        if (operator == '+') {
            answer = maths.add(inputA, inputB);
        }

        if (operator == '-') {
            answer = maths.subtract(inputA, inputB);
        }

        if (operator == '*') {
            answer = maths.multiply(inputA, inputB);
        }

        if (operator == '/') {
            answer = maths.divide(inputA, inputB);
        }

        if (operator == '^') {
            answer = maths.power(inputA, inputB);
        }

            System.out.println(answer);             
    }       

    input.close();

  }

}