gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:46:36 27 4
gpt4 key购买 nike

我是一名 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);




}

}

最佳答案

您的代码中有一些错误。

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

}

这行 this.result = result 没有任何意义。我认为您希望 WhatOperator() 方法返回结果,例如

this.result = other.whatOperator();

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

关于java - 为什么calculator.getValue()总是0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42618707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com