gpt4 book ai didi

Java - IF 语句仅返回 Else 子句

转载 作者:行者123 更新时间:2023-12-01 17:46:19 25 4
gpt4 key购买 nike

所以问题出在我的提款方法上,由于某种原因,我的代码输出:

ADL
826501829
250.0
471.0
Balance is currently:40.0
There aren't enough funds.
There aren't enough funds.

我对 Java 不太熟悉,所以不太明白为什么它没有从 IF 语句中输出正确的选项

public class BankAcc {

String accountName;
int accountNumber;
float accountOverdraft;
float currentBalance;

public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
System.out.println(accountName);
System.out.println(accountNumber);
System.out.println(accountOverdraft);
System.out.println(currentBalance);
}

public void deposit(float depositAmnt){
currentBalance = currentBalance + depositAmnt;
System.out.println("Balance is currently:" + currentBalance);
}

public void withdraw(float withdrawAmnt){
if (withdrawAmnt <= currentBalance){
currentBalance = currentBalance - withdrawAmnt;
System.out.println("Balance is currently:" + currentBalance);
} else {
System.out.println("There aren't enough funds.");
}
}

public static void main(String []args){
BankAcc myBankAcc = new BankAcc("ADL",826501829, 250, 471);
myBankAcc.deposit(40);
myBankAcc.withdraw(99);
myBankAcc.withdraw(999);
}
}

最佳答案

您需要在构造函数中设置属性,如下所示:

 public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
this.accountName = accountName;
this.accountNumber = accountNumber;
this.accountOverdraft = accountOverdraft;
this.currentBalance = currentBalance;
}

“this”指定您访问属性的当前类。

关于Java - IF 语句仅返回 Else 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54803160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com