为什么calculator.getValue()总是0?

时间:2017-03-06 06:13:38

标签: java

我是一名java学生,我正在努力使我的代码更加面向对象。我可以很容易地在main编写计算器,但我真的很难用方法来实现它。以下代码将始终返回0 ...但目标是创建一个程序,允许用户输入运算符和一行中的数字(例如+5)代码应输出前一个值,新值和允许重置。我相信我非常接近解决这个问题,只需要朝着正确的方向发展一点......

输出

Enter an operator and a number:
+5
0.0

计算器类

import java.util.Scanner;
public class Calculator {
    private final int RESET = 0;
    private double number = 0;
    private double result = 0; // I believe this is the issue but how can I resolve it?
    private char operator;
    private static Scanner keyboard = new Scanner(System.in);
    public Calculator(double number)
    {
        this.number = number;

    }
    // this method invokes the whatOperator() to create a new result
    // the edited method still returns 0
    public double aResult(Calculator other)
{

    other.whatOperator();
    this.result = other.result;
    return result;

} 
    // I created this method in hopes that it would do most of the work..when I invoke it and enter my operator and number it does not seem to function correctly
    public void whatOperator()
    {

        String operator = null;
        operator = enterNumber();
        double theNumber = Double.parseDouble(operator);
        char theOperator =operator.charAt(0);
        operator = null;
        operator += theOperator;

        // switch method to find the operator
        switch(operator){
        case "*":
        result = getNumber() * theNumber;
        break;
        case "/":
        result = getNumber() / theNumber;
        break;
        case "+":
        result = getNumber() + theNumber;
        break;
        case "-":
        result = getNumber() - theNumber;
        break;
        case "R":
        result = RESET;
        break;
    }


}
// methods for operation...I was hoping to not use these
public double add(double secondNumber)
{
    result = number + secondNumber;
    return result;

}
public double divide(double secondNumber)
{
    result = number / secondNumber;
    return result;
}
public double multiply(double secondNumber)
{
    result = number * secondNumber;
    return result;
}
public void subtract(double secondNumber)
{
    result = number - secondNumber;
}
public double getNumber()
{
    return number;
}
   // method for getting input
public static String enterNumber()
    {

        System.out.println("Enter an operator and a number:");
        String toString = keyboard.nextLine();
        return toString;
    }

    public static void main (String[] args) {
        // the calculator is initialized at 0
        Calculator a = new Calculator(0);
        // now I create a second calculator with the result from the aResult()
        Calculator b = new Calculator(a.aResult(a));
        // why is b.getNumber() = 0 at this point?
        String theString = String.valueOf(b.getNumber());
       // prints 0 every time
        System.out.println(theString);




        }

    }

3 个答案:

答案 0 :(得分:1)

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

this.result = other.whatOperator();

这条线。结果=结果没有任何意义。我想你想要方法whatOperator()返回一个结果,例如

li

我也认为你不想覆盖"其他"计算器。你永远不会使用新的计算器。但是您想在main方法中打印新计算器的输出。因为您从未使用过新计算器,所以输出为0。

答案 1 :(得分:0)

在你的aResult方法中,你正在启动另一个新的计算器实例

public double aResult(Calculator other) {
    //other = new Calculator(getNumber()); // this should not be here
    other.whatOperator();
    this.result = result;
    return result;

}

答案 2 :(得分:0)

解决问题的方法:

//change
 this.result = result; //this does nothing 
 //to
 this.result = other.result; //this changes the result to the new value
//erase this line
other = new Calculator(getNumber()); // do not need to create a new calculator

将方法whatOperator更改为double并使用它返回double