Java double.MAX_VALUE? [英] Java double.MAX_VALUE?

查看:211
本文介绍了Java double.MAX_VALUE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是计算机系统开发的第一年,所以我对Java很新,掌握了基础知识!

Hello there I'm in my first year of Computing System Development so I am pretty new to Java and getting to grips of the basics!

对于我的第一份工作,我必须为燃气公司创建一个燃气表系统,以允许员工创建新的客户帐户并修改名称和单位成本等数据从他们的账户中取款(存款)。

For my first assignment I have to create a Gas Meter System for a Gas company to allow employees to create new costumer accounts and amend data such as name and unit costs along with taking(depositing) money from their account.

我已经创建了我的构造函数,甚至添加了一个重载方法,尽管我在启动其中一个时遇到了问题我的方法我命名存款,这应该从用户帐户中取钱,而其他方法如 recordUnits 允许员工导入气体抄表客户使用了多少单位并更新客户账户的余额,这基本上是客户欠公司的。

I have created my constructor and even added in a method of overloading although I'm currently running into a problem when initiating one of my methods I named deposit, this is supposed to take money from the users account while other methods such as recordUnits allows the employee to import a gas meter reading of how many units the customer has used and updates the balance of that customers account which is essentially what the customer owes the company.

在尝试启动存款方式时使用预设信息测试程序时我得到了这个

When testing the program with just preset information when trying to initiate the deposit method I get this

Account.deposit(Double.MAX_VALUE);

我不太清楚这意味着什么,似乎无法找到它过去!如果已经发布,我会道歉,虽然我已经四处寻找合适的答案无济于事。

I am not too sure what this means and cannot seem to find anyway of getting past it! apologies if this has been posted although I have looked around to no avail for an appropriate answer.

测试数据和代码如下所示:

test data and code seen below:

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 strNewName, 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;

     }

}

这只是我的代码基本上还有很多东西要去,但这有希望给你一个想法。

This is just the bare bones of my code I have much more to go but this is will hopefully give you an idea.

提前谢谢你

推荐答案

Double.MAX_VALUE 是double可以表示的最大值(大约1.7 * 10 ^ 308)。

Double.MAX_VALUE is the maximum value a double can represent (somewhere around 1.7*10^308).

这应该以某些结尾计算问题,如果您尝试减去数据类型的最大可能值。

This should end in some calculation problems, if you try to subtract the maximum possible value of a data type.

即使在处理金钱时,也不应该使用浮点值,尤其是在舍入时会导致问题(您的系统中需要花费很多或更少的钱)然后)。

Even though when you are dealing with money you should never use floating point values especially while rounding this can cause problems (you will either have to much or less money in your system then).

这篇关于Java double.MAX_VALUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆