gpt4 book ai didi

java - 如何修改计算机 Balance() 方法以确保金额​​为正且不含分?

转载 作者:行者123 更新时间:2023-11-30 04:51:44 25 4
gpt4 key购买 nike

如何修改computeBalance()以确保JTextField中输入的金额为正数(我想我是这样做的)并且没有分?示例:100.19 和 -49.8 Not Acceptable 。

public class ATMbizlogic {

private double totalBalance;
private boolean rightPinEntered;



/**
* Creates a new instance of ATMbizlogic
*/
public ATMbizlogic()
{
totalBalance = 0.0;
rightPinEntered = true;
}

public void computeBalance(double withdraw, double deposit)
throws ATMexception
{
if(withdraw <=0)
throw new ATMexception("Negative withdraw not allowed");


if(deposit <=0)
throw new ATMexception("Negative deposit not allowed");


double balance = deposit - withdraw;

totalBalance = totalBalance + balance;



}



public void checkPin(double pin)
throws ATMexception
{
if(pin <=0)
throw new ATMexception("Negative pin not allowed");
if(rightPinEntered == false)
throw new ATMexception("Can not take another pin");
if(pin<1111 || pin>9999)
throw new ATMexception("Enter a valid pin");
rightPinEntered = true;


}




public double getBalance()
{
return totalBalance;
}
}

最佳答案

if(floor(withdraw) != withdraw)
throw new ATMexception("Withdraw with cents is not allowed.");

if(floor(deposit) != deposit)
throw new ATMexception("Deposit with cents is not allowed.");

将数字向下舍入并检查它们是否仍然是相同的数字。如果不是,则表示它有小数。

关于java - 如何修改计算机 Balance() 方法以确保金额​​为正且不含分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9691524/

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