2017-03-06 125 views
0

我是一名java学生,我正致力于让我的代码更加面向对象。我可以很容易地在主代码计算器,但我真的很努力用方法来实现它。以下代码将始终返回0 ...但目标是创建一个允许用户在一行中输入运算符和数字的程序(示例+5),代码应该输出前一个值,新值以及允许重置。我相信,我真的很接近解决这一并且只需要一个点在正确的方向..为什么calculator.getValue()总是0?

输出

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); 




     } 

    } 
+2

在您发布的代码中没有'getNumber()'方法。 – shmosel

+1

in aResult'this.result = result' does not nothing,it should read'this.result = other.aResult()' – Turo

+0

在一般说明中,为每个计算步骤创建一个新的'Calculator'非常奇怪。应该只有一个实例。 – shmosel

回答

1

有在你的代码的一些错误。

public double aResult(Calculator other) 
{ 
    other = new Calculator(getNumber()); 
    other.whatOperator(); 
    this.result = result; 
    return result; 

} 

该行this.result =结果没有任何意义。我想你想用whatOperator()来返回结果的方法,例如

this.result = other.whatOperator(); 

我也认为你不想重写“其他”计算器。你从不使用新的计算器。但是你想在主要方法中打印新计算器的输出。因为你从来没有使用过的新的计算器,输出为0

+1

whatOperator is void – Turo

+0

是的,这也必须改变 – Markus

+0

,这是绝对有道理的。所以我会改变whatOperator()方法来加倍? – mark1092

0

在你aResult方法,则需要启动计算器

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

} 
+0

是的,上面的评论指出,我...纠正它,但输出是相同的 – mark1092

+0

另一个更正whatOperator方法赋值operator =“”之前 operator + =操作符;否则,运算符变量被赋值为空+并且没有满足开关条件。 –

+0

解决了!我不确定谁在这里信誉,你们都帮助我 – mark1092

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的方法

相关问题