你好,我在我计算系统开发的第一年,所以我对
Java很新,掌握了基础知识!
对于我的第一个任务,我必须为燃气公司创建一个燃气表系统,以允许员工创建新的客户帐户,并修改数据,如名称和单位成本以及从账户中存入(存入)资金.
我创建了我的构造函数,甚至添加了一个超载的方法,虽然我正在遇到一个问题,当我启动我的一个方法我命名存款,这应该是从用户帐户的钱,而其他方法,如recordUnits允许员工进口一个燃气表,读取客户使用的单位数量,并更新客户帐户的余额,这实际上是客户欠公司的.
当试图启动存款方法时,只需要预设的信息来测试程序,我得到这个
Account.deposit(Double.MAX_VALUE);
我不太确定这是什么意思,似乎无法找到过去吧!道歉,如果这已经发布,虽然我周围没有看到适当的答案.
测试数据和代码如下:
public class TestGasAccount { public static void main (String [] args) { GasAccount Account = new GasAccount (223,"Havana","TQ",1000); Account.getAccNo(); Account.getName(); Account.getAddress(); Account.getUnits(); Account.getBalance(); Account.recordUnits(1000); Account.getUnits(); Account.getBalance(); Account.deposit(Double.MAX_VALUE); } }
打破
public class GasAccount { private int intAccNo; private String strName; private String strAddress; private double dblBalance; private double dblUnits; protected double dblUnitCost = 0.02; public GasAccount(int intNewAccNo,String strNewName,String strNewAddress,double dblNewUnits) { intAccNo = intNewAccNo; strName = strNewName; strAddress = strNewAddress; dblUnits = dblNewUnits; dblBalance = dblNewUnits * dblUnitCost; } public GasAccount (int intNewAccNo,String strNewAddress) { intAccNo = intNewAccNo; strName = strNewName; strAddress = strNewAddress; } public double deposit (Double dblDepositAmount) { dblBalance = dblBalance - dblDepositAmount; return dblBalance; } public String recordUnits (double dblUnitsUsed) { double dblTempBalance; dblTempBalance = dblUnitsUsed * dblUnitCost; dblBalance = dblBalance + dblTempBalance; dblUnits = dblUnits + dblUnitsUsed; return "Transaction Successful"; } public int getAccNo () { System.out.println(intAccNo); return intAccNo; } public String getName() { System.out.println(strName); return strName; } public String getAddress() { System.out.println(strAddress); return strName; } public double getBalance() { System.out.println("£"+dblBalance); return dblBalance; } public double getUnitCost() { return dblUnitCost; } public double getUnits () { System.out.println(dblUnits); return dblUnits; } public void updateUnitCost (double dblNewUnitCost) { dblUnitCost = dblNewUnitCost; } }
这只是我的代码的裸骨,我有更多的要去,但这将是希望给你一个想法.
先谢谢你
解决方法
Double.MAX_VALUE是双重表示的最大值(约1.7 * 10 ^ 308左右).
如果您尝试减去数据类型的最大可能值,则应该在某些计算问题中结束.
即使在处理金钱的时候,也不应该使用浮点值,特别是在舍入这个问题时可能会导致问题(那么您的系统中的资金要少得多).