我应该如何修复我的循环?

时间:2015-03-10 03:38:52

标签: java

我正在开设银行账户。似乎我得到了正确的逻辑,但它没有产生正确的输出。我认为循环不明白我想告诉它做什么。我是否将方法置于错误的位置?我附上了一个示例输出。我感谢你的帮助!谢谢

 /**
 * This program prints a payoff schedule for a bank customer.
  Input: Account number, name of customer, and account balance.
  Output: Prints payoff schedule that includes payment amount and new balance
  until the account will be paid off. A customer must pay off their account when
  the balance reaches $10 or less. 1.2% interest is added at the beginning of
  each month and the customer then makes a payment equal to 5% of the current balance. 
  */

public class DT_BankAccount
{
 private String accountNum;
 private String customerName;
 private double customerBalance;
 double interest = 0.00; // Start out with 0.00 value for the customer.
 double payment = 0.00; // Start out with 0.00 value for the customer.

/**
* Constructor: Constructs a payoff schedule for a bank customer including account number, name of customer, and account balance.
* @param account number - the account number of the customer
* @param name of customer - the customer's name of the bank account
* @param account balance - the account balance of the customer
*/

public DT_BankAccount(String accountNum, String customerName, double customerBalance)
{
 // Initialize constructors
    this.accountNum = accountNum; 
    this.customerName = customerName;
    this.customerBalance = customerBalance;
}

 /**
  * This method calculates interest and return the balance times the 1.2% interest: the balance times the 1.2% interest
  * @return the balance times the 1.2% interest
  */
public double CalculateInterest()
{ 
  return interest = customerBalance * 0.012;
}

   /**
   * This method calculates new balance adds the interest to the balance
   */
 public void CalculateNewBalance()
 {
   payment = customerBalance + interest;
 }

 /**
  * This method calculates new payment returns the balance times 5%: the balance times 5% 
  * @return the balance times 5% 
  */
public double CalculatePayment()
{ 
  return payment * 0.05;
}

 /**
  * This method updates the balance after the payment is made: the updated (newest) balance
  * @return the updated (newest) balance
  */
public double BalanceUpdated()
{ 
  return customerBalance = customerBalance + interest - payment;
}
}

 import javax.swing.JOptionPane; // import JOptionPane class

 public class DT_PayoffTester
{
public static void main(String args[])
{
    // Declare and initialize variables.
    String accountNum;
  String customerName;
  String stringCustomerBalance;
  double customerBalance = 0.00;
    double controller = 10.00; // Controls while loop.


    // Get input.
    accountNum = JOptionPane.showInputDialog("Enter account number: ");
    customerName = JOptionPane.showInputDialog("Enter customer's name: ");
    stringCustomerBalance = JOptionPane.showInputDialog("Enter customer's balance: ");

  // Convert string to double.
    customerBalance = Double.parseDouble(stringCustomerBalance);

  // creates one instance of the BankAccoount class by calling it’s constructor.
  DT_BankAccount BA = new DT_BankAccount(accountNum,customerName,customerBalance);

    System.out.println(customerName + "\n");

    while (BA.BalanceUpdated() > controller)
  {
        // methods called
        BA.CalculateInterest();
        BA.CalculateNewBalance();
        BA.CalculatePayment();
        BA.BalanceUpdated();
        System.out.println("Payment: " + BA.CalculatePayment());
        System.out.println("New Balance: " + BA.BalanceUpdated() + "\n");
    } // End of while loop.
    System.out.println("Account Number: " + accountNum);
    System.out.println("Final Balance Amount: " + BA.BalanceUpdated());
    System.out.println("Loan Paid Off");

    System.exit(0);
   }
  }

示例输出:

Start of sample output 
Billie Gates
Payment: $12.02
New Balance: $228.39
Payment: $11.56
New Balance: $219.57
Payment: $11.11
New Balance: $211.09
Payment: $10.68
New Balance: $202.95
Payment: $10.26
New Balance: $195.11
Payment: $9.87
New Balance: $187.58
Payment: $9.49
New Balance: $180.34

跳到示例输出的末尾:

Payment: $ 0.62
New Balance: $11.92
Payment: $ 0.60
New Balance: $11.46
Payment: $0.58
New Balance: $11.02
Payment: $0.55
New Balance: $10.59
Payment: $0.53
New Balance: $10.18
Payment: $0.51
New Balance:$ 9.79
Account Number: 91234567
Final Balance Amount:$ 9.79
Loan Paid Off

我的输出(错误的)

 Billie Gates

 Payment: 6.229872
 New Balance: -123.12

 Account Number: 123
 Final Balance Amount: -369.36
 Loan Paid Off

2 个答案:

答案 0 :(得分:1)

数学来了:

输入:237.56

循环1:

  

BA.BalanceUpdated()循环的while中,customerBalance设置为customerBalanceinterestpayment仍为0. {{1 }是237.56,这是> 10.00,循环继续。

     

customerBalance中,BA.CalculateInterest()设置为237.56 x 0.012 =   2.85072

     

interest中,BA.CalculateNewBalance()设置为237.56 + 2.85072 =   240.41072

     

payment中,它在第一次通话时无效(就像你一样)   不要使用返回的值。)

     

在第二个BA.CalculatePayment()中,它返回到240.41072 x 0.05 = 12.020536,但它不会分配给任何变量。

     

在打印行的第二个BA.CalculatePayment()中,BA.BalanceUpdated()设置为240.41072 +   2.85072 - 240.41072 = 2.85072

循环1完成

然后,customerBalance检查while是否返回> 10.00,此时你第二次执行这个功能,将BA.BalanceUpdated()设置为2.85072 + 2.85072 - 240.41072 = -234.70928,这是< 10.00。 customerBalance循环中断。

因此,检查你的数学逻辑。

答案 1 :(得分:0)

第一点:

在这种情况下,我的一般建议是进行桌面检查&#39 ;;使用笔和纸在一些样本数据上手动执行代码。也就是说,从某个值开始,遍历您的代码,每行执行计算并记录每行后的所有变量的状态。这可能很耗时,但确实有效。

第二点:

您似乎没有意识到,每次拨打BalanceUpdated()时,customerBalance的值都会被更改。如果您在循环条件下调用该方法,或者如果您在System.out.println()内调用它,它仍会更改该值。在单个执行此while循环结束时,已添加兴趣并减去三次付款。