有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java余额=10,但减去20?

我试图在课堂上使用一种收费的方法。方法如下:

  public double chargeFee()
  {
    balance -= 10;
    return balance;
  }

然而,它减去20。我尝试过重新编译它,但找不到导致问题的原因

完整代码:

public class ManageAccounts
{
    public static void main(String[] args)
    {
     Account acct1, acct2; 


    //create account1 for Sally with $1000
    acct1 = new Account(1000, "Sally", 1111);
    acct2 = new Account(500, "Joe", 2222);//create account2 for Joe with $500

    System.out.println("Depositing $100 into Account 2222...");
    acct2.deposit(100.00);//deposit $100 to Joe's account
    System.out.println("New Balance for Account 2222: $" + acct2.getBalance());//print Joe's new balance (use getBalance())
    System.out.println();
    System.out.println("Withdrawing $50 from Account 1111...");
    acct1.withdraw(50);//withdraw $50 from Sally's account

    System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's new balance (use getBalance())
    System.out.println();
    acct1.chargeFee();
    acct2.chargeFee();//charge fees to both accounts
    System.out.println("Charging usage Fees...");
    System.out.println("Account balance after fees:");
    System.out.println("Account 1111: $" + acct1.chargeFee());
    System.out.println("Account 2222: $" + acct2.chargeFee());
    System.out.println();
    System.out.println("Changing name on Account 2222...");
    acct2.changeName("Joseph");//change the name on Joe's account to Joseph
    System.out.println();
    System.out.println("Printing account summaries...");
    System.out.println(acct1.toString());
    System.out.println(acct2.toString());//print summary for both accounts

    }
}



import java.text.NumberFormat;

public class Account
{
  private double balance;
  private String name;
  private long acctNum;
    NumberFormat money = NumberFormat.getCurrencyInstance();
  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return ("Name: " + name + "\tAccount Number: " + acctNum + "\tBalance: " + money.format(balance));
  }

  //----------------------------------------------
  // Deducts $10 service fee
  //----------------------------------------------
  public double chargeFee()
  {
    balance -= 10;
     return balance;
  }

  //----------------------------------------------
  // Changes the name on the account 
  //----------------------------------------------
  public void changeName(String newName)                          
  {
    name = newName;
  }

}

一些示例输出:

Depositing $100 into Account 2222...
New Balance for Account 2222: $600.0

Withdrawing $50 from Account 1111...
New Balance for Account 1111: $950.0

Charging usage Fees...

Account balance after fees:

Account 1111: $930.0
Account 2222: $580.0

Changing name on Account 2222...

Printing account summaries...
Name: Sally Account Number: 1111    Balance: $930.00
Name: Joseph    Account Number: 2222    Balance: $580.00

共 (5) 个答案

  1. # 1 楼答案

    你已经有几个正确的答案指出了你程序中的问题。这并不是一个真正的答案,而是一个关于你自己是如何发现这个问题的延伸评论

    给出一个理论,即chargeFee方法减去20,下一步是添加断点或打印输出,以检查它的实际功能:

    public double chargeFee() {
      System.out.println("Before subtract: " + balance + " Account: " + acctNum);
      balance -= 10;
      System.out.println("After subtract: " + balance + " Account: " + acctNum);
      return balance;
    }
    

    该程序的输出和额外的打印输出表明,每次调用chargeFee只减去10,但每个帐户调用两次

  2. # 2 楼答案

    你在两个账户上打了两次电话chargeFee,一次是在收取实际费用时,另一次是在打印结果时

    为了将来参考,有一个叫做"Seperate command and query"或CQS的指南。你可能想好好读一读。要了解你的情况:

    • 您使用chargeFee作为查询(即返回余额)
    • 你用chargeFee作为一个命令,扣钱

    更改chargeFee以返回一个void(或者this,如果需要方法链接),就不会意外地将其用作查询

    http://en.wikipedia.org/wiki/Command-query_separation

  3. # 3 楼答案

    System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's   new balance (use getBalance())
    System.out.println();
    acct1.chargeFee();------------------------//First time
    acct2.chargeFee();//charge fees to both accounts
    System.out.println("Charging usage Fees...");
    System.out.println("Account balance after fees:");
    System.out.println("Account 1111: $" + acct1.chargeFee());------------------------//second time
    System.out.println("Account 2222: $" + acct2.chargeFee());
    
  4. # 4 楼答案

    它为每个账户扣除2笔费用

    acct1.chargeFee();
    acct2.chargeFee();//charge fees to both accounts
    System.out.println("Charging usage Fees...");
    System.out.println("Account balance after fees:");
    System.out.println("Account 1111: $" + acct1.chargeFee());
    System.out.println("Account 2222: $" + acct2.chargeFee());
    

    可以说,chargeFee()不应该返回余额,而是像deposit()withdraw()一样。还请注意,财务事项通常以int而不是double数字处理

  5. # 5 楼答案

    1) 
    acct1.chargeFee();
    acct2.chargeFee();//charge fees to both accounts
    
    2)
    System.out.println("Account 1111: $" + acct1.chargeFee());
    System.out.println("Account 2222: $" + acct2.chargeFee());
    

    注意您如何调用chargeFee()两次。这就是给你带来混乱结果的原因。你只需要打一次电话,然后第二次打印每个账户的余额(第二部分)

    System.out.println("Account 1111: $" + Account 1111's balance);
    System.out.println("Account 2222: $" + Account 2222's balance);
    

    希望有帮助