所以问题出在我的withdraw方法上,出于某种原因,我的代码正在输出:
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);
}
}
答案 0 :(得分:4)
您需要像这样在构造函数中设置属性:
public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
this.accountName = accountName;
this.accountNumber = accountNumber;
this.accountOverdraft = accountOverdraft;
this.currentBalance = currentBalance;
}
“ this”表示您从中访问属性的当前类。
答案 1 :(得分:0)
class TransactionType(Enum):
IN = "IN"
OUT = "OUT"
@classmethod
def choices(cls):
print(tuple((i.name, i.value) for i in cls))
return tuple((i.name, i.value) for i in cls)
class TransactionStatus(Enum):
INITIATED = "INITIATED"
PENDING = "PENDING"
COMPLETED = "COMPLETED"
FAILED = "FAILED"
ERROR = "ERROR"
@classmethod
def choices(cls):
print(tuple((i.name, i.value) for i in cls))
return tuple((i.name, i.value) for i in cls)
您打印传递的值,但实际上没有设置它们。
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);
}
您通过了471,这应该进入currentBalance,但这并不意味着在您存入40并尝试提取99后,您的资金确实不足。
将构造函数更改为:
BankAcc myBankAcc = new BankAcc("ADL",826501829, 250, 471);
答案 2 :(得分:0)
您没有保存放入构造函数中的值
public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
this.accountName = accountName;
this.accountNumber = accountNumber;
this.accountOverdraft = accountOverdraft;
this.currentBalance = currentBalance;
System.out.println(accountName);
System.out.println(accountNumber);
System.out.println(accountOverdraft);
System.out.println(currentBalance);
}
答案 3 :(得分:0)
您的代码“正常工作”。您正在将存款(40)与99或999进行比较,因此40-99将像“ 40-999”一样“被发现的数量不足”。
我建议您使用getter和setter,因为稍后您对代码会感到困惑。