作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以问题出在我的提款方法上,由于某种原因,我的代码输出:
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/
我是一名优秀的程序员,十分优秀!