所以,作为免责声明,我对编程非常陌生,可能会遗漏一些明显的东西。现在,我试图创建两个名为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;
}
}
}
我正在使用DrJava,我正在交互面板中测试我的代码。这是我测试的结果。
> Trader t1
> t1 = new Trader("Bill")
Trader@22e1cbe4
> t1.deposit(10.0)
10.0
> t1.getBalance()
0.0
我已经完成了我可以想象的所有修复代码的事情,但我没有想法,我不认为在我的代码中再输入3个小时的随机内容会做很多事情。
感谢您抽出宝贵时间阅读我的问题。
答案 0 :(得分:2)
下面的代码工作正常。我不确定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());
}
}
输出:
Balance before deposit:
0.0
Balance after deposit:
10.0
答案 1 :(得分:2)
班级“Trader”看起来很好并为我工作。但是你有一个“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());
}
}