gpt4 book ai didi

java - 如何设置 overdraw 限额

转载 作者:行者123 更新时间:2023-11-29 06:55:01 26 4
gpt4 key购买 nike

我想做的是设置 overdraw 限额,即 -150 如果余额为 -150,用户将无法再提款。我的代码如下:

提现方式

public void Withdraw(double amount){


if (amount + 5 > balance ){ //If amount and transaction fee is greater than balance then apply for overdraft
System.out.println("Insufficent funds");
System.out.println("Would you like to apply for an ovedraft?");
System.out.println("1:Yes");
System.out.println("2:No, return me back to menus");
Choice = Option.nextLine();
if (Choice.equalsIgnoreCase("1")){
if((balance = balance - amount+5)<=-150){ //If balance is grater than 150 , apply for overdraft
System.out.println("You have exceeded your Overdraft Limit, you will now be returned back to the menus");
return;

}
else{ //if not exceeding bank balance
balance -= amount + 5;
System.out.println("You have withdrawen £" + amount);
System.err.println("You now have a balance of £" +balance);
return;
}
}
}

}

它们都在同一个类中,即“帐户”,现在发生的是消息确实发生了-(“您已经超过了 overdraw 限额,您现在将返回到菜单”)并返回到菜单但是当我去检查余额时,钱仍然被扣除并显示超过-150的余额,例如。 -190 我怎样才能使 -150 是限制而不是扣除更多。希望这个问题得到理解。

最佳答案

问题是:

if ((balance = balance - amount + 5) <= -150) {

这里的关键是 = .即在支票之前分配新金额。

这样的事情可能会避免这个问题。

    if (balance - amount - 5 < 0) {
System.out.println("Insufficent funds");
System.out.println("Would you like to apply for an ovedraft?");
System.out.println("1:Yes");
System.out.println("2:No, return me back to menus");
Choice = Option.nextLine();
if (Choice.equalsIgnoreCase("1")) {
if (balance - amount - 5 <= -150) {
System.out.println("You have exceeded your Overdraft Limit, you will now be returned back to the menus");
} else { //if not exceeding bank balance
balance -= amount + 5;
System.out.println("You have withdrawen £" + amount);
System.err.println("You now have a balance of £" + balance);
}

请注意更明确的计算 ( if (balance - amount - 5 <= -150) ),它使用 <= 0 模拟数学和比较.这通常更容易理解。

关于java - 如何设置 overdraw 限额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160204/

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