我是一名java学生,我正在努力使我的代码更加面向对象。我可以很容易地在main中编写计算器代码,但我真的很难用方法来实现它。以下代码将始终返回0...but目标是创建一个程序,该程序允许用户在一行(例如+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);
}
}发布于 2017-03-06 14:21:26
您的代码中有一些错误。
public double aResult(Calculator other)
{
other = new Calculator(getNumber());
other.whatOperator();
this.result = result;
return result;
} 行this.result = result没有任何意义。我想您希望whatOperator()方法返回一个结果,例如
this.result = other.whatOperator();我也认为你不想覆盖“其他”计算器。你从来不用新的计算器。但是您希望在main方法中打印新计算器的输出。由于您从未使用过新的计算器,因此输出为0。
发布于 2017-03-06 14:31:27
在aResult方法中,您正在启动另一个新的计算器实例
public double aResult(Calculator other) {
//other = new Calculator(getNumber()); // this should not be here
other.whatOperator();
this.result = result;
return result;
}发布于 2017-03-06 14:56:00
问题的解决方案:
//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
https://stackoverflow.com/questions/42618707
复制相似问题