首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么calculator.getValue()总是0?

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

Stack Overflow用户
提问于 2017-03-06 14:13:38
回答 3查看 115关注 0票数 0

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

输出

代码语言:javascript
复制
Enter an operator and a number:
+5
0.0

计算器类

代码语言:javascript
复制
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);




        }

    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-06 14:21:26

您的代码中有一些错误。

代码语言:javascript
复制
public double aResult(Calculator other)
{
    other = new Calculator(getNumber());
    other.whatOperator();
    this.result = result;
    return result;

} 

行this.result = result没有任何意义。我想您希望whatOperator()方法返回一个结果,例如

代码语言:javascript
复制
this.result = other.whatOperator();

我也认为你不想覆盖“其他”计算器。你从来不用新的计算器。但是您希望在main方法中打印新计算器的输出。由于您从未使用过新的计算器,因此输出为0。

票数 1
EN

Stack Overflow用户

发布于 2017-03-06 14:31:27

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

代码语言:javascript
复制
public double aResult(Calculator other) {
    //other = new Calculator(getNumber()); // this should not be here
    other.whatOperator();
    this.result = result;
    return result;

}
票数 0
EN

Stack Overflow用户

发布于 2017-03-06 14:56:00

问题的解决方案:

代码语言:javascript
复制
//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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42618707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档