在Java中更改字段值时遇到问题

在Java中更改字段值时遇到问题,java,Java,因此,作为免责声明,我对编程非常陌生,可能遗漏了一些显而易见的东西。现在,我正在尝试创建两个名为draw()和deposit()的方法,这两个方法允许我更改字段currentBalance的值,但是,每次我尝试使用这两个方法更改currentBalance的值时,我都会使用方法getBalance()检查字段的值,并且始终保持不变 class TradingService{ public class Trader { //This field stores the trad

因此,作为免责声明,我对编程非常陌生,可能遗漏了一些显而易见的东西。现在,我正在尝试创建两个名为draw()和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个小时会有什么效果


感谢您抽出时间阅读我的问题。

下面的代码工作正常。我不确定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
“交易者”班看起来不错,为我工作。但是你有一个“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());

    }
}

看来它会起作用的。
Trader
class对我来说很合适。你能发布你实际运行测试、创建trader、调用getBalance()等的主要方法吗?我对Java博士不熟悉,但这可能是你忘记保存之类的愚蠢行为吗?除非你保存,否则代码实际上不会编译,所以没那么简单,很遗憾。@user6933987您确定发布的类与您试图运行的类完全相同吗?您的示例代码不能与您发布为
Trader
的类一起工作,该类是
TradingService
的内部非静态类,您不能像示例中那样直接实例化它。我下载了Dr.Java,没有外部类(
TradingService
),您的代码按预期工作,并在调用
getBalance()
后打印回10.0。非常感谢你!