[英]Having Trouble with Changing the Value of a Field in Java
So, as a disclaimer, I am extremely new to programming and am probably missing something obvious. 因此,作为免责声明,我对编程非常陌生,可能会遗漏一些明显的东西。 Now, I am trying to create two methods named withdraw() and deposit() that will allow me to change the value of the field currentBalance, however, every time I try to change the value of currentBalance with my two methods, I check the value of the field with my method getBalance() and it is always unchanged. 现在,我试图创建两个名为withdraw()和deposit()的方法,这将允许我更改字段currentBalance的值,但是,每次我尝试用我的两个方法更改currentBalance的值时,我检查使用我的方法getBalance()的字段值,它始终保持不变。
class TradingService{
public class Trader {
//This field stores the trader's name
private String traderName;
//This field stores the trader's current balance
private double currentBalance;
//A constructor to create a Trader
public Trader(String traderName) {
this.traderName = traderName;
}
//This method gets returns the trader's name
public String getName() {
return traderName;
}
//This method set's the trader's name
public void setName(String traderName) {
this.traderName = traderName;
}
//This method decreases the trader's balance
public void withdraw(double withdrawAmount) {
this.currentBalance = (currentBalance - withdrawAmount);
}
//This method increases the trader's balance
public void deposit(double depositAmount) {
this.currentBalance = (currentBalance + depositAmount);
}
//This method returns the trader's current balance
public double getBalance() {
return currentBalance;
}
}
}
I am using DrJava and I am testing my code in the interactions panel. 我正在使用DrJava,我正在交互面板中测试我的代码。 Here is the result of my tests. 这是我测试的结果。
> Trader t1
> t1 = new Trader("Bill")
Trader@22e1cbe4
> t1.deposit(10.0)
10.0
> t1.getBalance()
0.0
I have done everything that I can imagine to fix the code, but I am out of ideas and I don't think that typing random things into my code for another 3 hours will do much. 我已经做了我可以想象的所有修复代码的事情,但我没有想法,我不认为在我的代码中再输入3个小时的随机内容会做很多事情。
Thank you for taking the time to read my question. 感谢您抽出宝贵时间阅读我的问题。
Below code is working fine. 下面的代码工作正常。 I am not sure what Dr. Java is but the code is working as expected. 我不确定Java博士是什么,但代码正在按预期工作。
public class TradingService {
class Trader {
//This field stores the trader's name
private String traderName;
//This field stores the trader's current balance
private double currentBalance;
//A constructor to create a Trader
public Trader(String traderName) {
this.traderName = traderName;
}
//This method gets returns the trader's name
public String getName() {
return traderName;
}
//This method set's the trader's name
public void setName(String traderName) {
this.traderName = traderName;
}
//This method decreases the trader's balance
public void withdraw(double withdrawAmount) {
this.currentBalance = (currentBalance - withdrawAmount);
}
//This method increases the trader's balance
public void deposit(double depositAmount) {
this.currentBalance = (currentBalance + depositAmount);
}
//This method returns the trader's current balance
public double getBalance() {
return currentBalance;
}
}
public static void main(String[] args) {
TradingService service = new TradingService();
TradingService.Trader trader = service.new Trader("trader");
System.out.println("Balance before deposit:\n" + trader.getBalance());
trader.deposit(10.00);
System.out.println("Balance after deposit:\n" + trader.getBalance());
}
}
Output: 输出:
Balance before deposit:
0.0
Balance after deposit:
10.0
The class "Trader" looking fine and work for me. “Trader”这个班级很好看,适合我。 But you have a "Trader" what is a public class inside "TradingService" class, I'm thinks maybe the class is not compiling and you are executing an old file, you can try this with "Trader" class like a inner class. 但是你有一个“Trader”什么是“TradingService”类中的公共类,我认为这个类可能没有编译而且你正在执行一个旧文件,你可以像“内部类”一样尝试使用“Trader”类。
class Trader {
//This field stores the trader's name
private String traderName;
//This field stores the trader's current balance
private double currentBalance;
//A constructor to create a Trader
public Trader(String traderName) {
this.traderName = traderName;
}
//This method gets returns the trader's name
public String getName() {
return traderName;
}
//This method set's the trader's name
public void setName(String traderName) {
this.traderName = traderName;
}
//This method decreases the trader's balance
public void withdraw(double withdrawAmount) {
this.currentBalance = (currentBalance - withdrawAmount);
}
//This method increases the trader's balance
public void deposit(double depositAmount) {
this.currentBalance = (currentBalance + depositAmount);
}
//This method returns the trader's current balance
public double getBalance() {
return currentBalance;
}
}
public class TradingService{
public static void main(String[] args)
{
Trader t = new Trader("test");
t.deposit(10);
System.out.print(t.getBalance());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.