我試圖計算一個簡單的銀行function.I通過兩個撤回值150和47.62通過一個方法從執行類到另一個。但它需要兩次47.62兩次,並給我錯誤的結果這裏是執行類。給出錯誤的結果,我該怎麼辦?
public class Account {
public double balance ;
public double deposite;
public double withdraw;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getDeposite() {
balance = balance + deposite;
return deposite;
}
public void setDeposite(double deposite) {
this.deposite = deposite;
}
public double getWithdraw() {
return withdraw;
}
public void setWithdraw(double withdraw) {
this.withdraw = withdraw;
if(withdraw <= balance){
balance = balance - withdraw;
}
}
public boolean withdraw(double wamt)
{
boolean result = false;
if(withdraw <= wamt)
{
balance= balance - withdraw;
return true;
}
return result;
}
}
我的客戶類
public class Customer {
private String firstName;
private String lastName;
Account account;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
//this.account = account;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account; }
}
主要類:
public class BankProjectDemo {
public static void main(String[] args) {
double balance = 500;
Customer cust = new Customer("asasd0","asdasda");
Account accnt = new Account(balance);
System.out.println("Creating customer: " +cust.getFirstName());
accnt.setWithdraw(150);
accnt.setDeposite(22.50);
System.out.println("Withdraw1 "+accnt.getWithdraw());
System.out.println("Depsoite "+accnt.getDeposite());
Account accnt1 = new Account(balance);
accnt1.setWithdraw(47.62);
System.out.println("Withdraw2 "+accnt1.getWithdraw()+" " + accnt1.withdraw(balance));
System.out.println("Balance " + accnt.getBalance());
}
}
Account.withdraw()方法ds返回一個布爾結果。兩個方法是相同的,但一個是布爾類型和其他是無效類型。我的問題是結果餘額應該是324.88作爲總餘額,但它給出277.26。它需要第二次提取金額兩次,但爲什麼?????????? – user2349171
調試器:「你好,請用我吧:(」。 – Maroun
你試過調試它嗎?學習如何調試比請求別人爲你做得更好 – BobTheBuilder