2017-03-06 1 views
0

저는 자바 학생이며 제 객체 지향적 인 객체를 만들기 위해 노력하고 있습니다. 나는 쉽게 계산기를 주 코드화 할 수 있지만, 나는 그것을 방법으로 구현하는 데 정말로 어려움을 겪고있다. 다음 코드는 항상 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

aResult'this.result = result'는 아무 것도하지 않고,'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 = result는 의미가 없습니다. whatOperator() 메서드에서 결과를 반환하기를 원한다고 생각합니다.

this.result = other.whatOperator(); 

나는 또한 "다른"계산기를 무시하고 싶지 않다고 생각합니다. 당신은 새로운 계산기를 사용하지 않습니다. 그러나 주 계산기로 새로운 계산기의 결과를 출력하고 싶습니다.

//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 

변화 : 새 계산기를 사용하지 않기 때문에, 출력은 aResult 방법에서 0

+1

whatOperator는 무효입니다. – Turo

+0

네,이 또한 분명히 의미가있는 – Markus

+0

으로 변경해야합니다. 그래서 whatOperator() 메서드를 double로 변경하겠습니까? – mark1092

0

당신이 계산기

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 + = theOperator; 그렇지 않으면 연산자 변수에 null +가 할당되고 스위치 조건이 충족되지 않습니다. –

+0

해결! 나는 누가 여기에 신용을 줄 것인지 확신하지 못한다. 너희들 모두 나를 도왔다. – mark1092

0

문제에 대한 해결책의 또 다른 새로운 인스턴스를 시작하는 것입니다 whatOperator를 double로 변환하고 double을 반환하십시오.

관련 문제