【问题标题】:Converting from int to double in java在java中从int转换为double
【发布时间】:2014-08-10 12:08:10
【问题描述】:

以下代码计算自动售货机分发的零钱。我的问题?由于两种不同的数据类型(int & double 转换),我无法让 change 变量工作,因为编译器不会让我工作。谁能帮我解决这个问题。 我试过铸造“改变”,但它不会打印正确的数量。 例如,如果零钱是 0.25 美分,零钱价值保持为零……当然,原因很明显。问题从第 16 行开始。我将给出示例的部分注释为 change = 0.25。

public String[] itemList = new String[] {"Water   ","Coke    ", "Diet Coke", "Iced Tea","Fanta   "};
public double[] priceList = new double[] {75,120, 120, 100, 150};
public int[] itemQty = new int[]{10,10,10,10,10};
public int[] coinList = new int[]{100,50,20,10,5};
public int[] coinQty = new int[]{10,10,10,10,10};
public double change;
public double paid;

    public void ReturnChange()
{
    int Denominations=5;
    int coins_dispensed = 0 ;
    int[] InitialArray = new int[Denominations];

    //My Problem begins here..for example if change is computed
      change = 0.25; //change is a global declaration of type double and carries values derived from different function
      int change1 = (int)change; //if i cast here, i get change as 0, thus the part that follows, fails to compute coins dispensed.

    for (int i=0; i < 5; i++)               
    {
    InitialArray[i] += coinQty[i];  // Copies Coin Quantity to Initial array for difference 

    }

    System.out.println("Your change is  "+NumberFormat.getCurrencyInstance().format(Math.abs(change1)) +" which comprises of:"); //OK till here


    for (int i=0; i<5; i++)
    {
        if (coinQty[i]>0) //if a particular denomination is available
        {
            coins_dispensed = (change1/coinList[i]); //dividing coins dispense with denomination
            coinQty[i] -= coins_dispensed; //reduce the quantity of the denomination dispensed
            change1 = change1 - (coinList[i] * coins_dispensed); //total the change
        }
        else                                            // Moves to next denomination if a particular coin runs out
        {
            coins_dispensed = (change1/coinList[i+1]);
            coinQty[i+1] -= coins_dispensed ;
            change1 = change1 - (coinList[i+1] * coins_dispensed);
        }
    }
    if (change1 != 0)                                   // In the case not enough coins to make change, selection is ignored. 
    {
        System.out.println("\n\n\t Sorry. The machine doesnt have enough coins to make up your change. Your last transaction has been ignored.");
    }
    else 
    {
        for (int i=0; i<Denominations; i++)
        {
            coins_dispensed = InitialArray[i] - coinQty[i];
            System.out.println( "\n\t\t\t" + coins_dispensed +" of "+ coinList[i] + "  cents coins");
        }
    }


}

【问题讨论】:

  • 您能否指出您的问题发生在哪一行以及尝试投射到哪里?
  • 您的代码中缺少某些部分。也请贴出coinList和coinQty的声明和初始化代码。
  • 根据要求,我已经更新了代码。请再看看它,让我知道你们是否可以帮助我。确实会很感激。

标签: java int double


【解决方案1】:

您应该在任何地方使用integers,但以美分而不是美元计算。打印时只需将数字除以 100。

这是因为 floatsdoubles 不能准确地表示用于货币的以 10 为底的倍数,并且会引入舍入误差,尤其是在乘以计算利率时。

有关更多信息和讨论,请参阅Why not use Double or Float to represent currency?

【讨论】:

  • 显然,我已经尝试过上面提到的方法,但它不适用于分发硬币。
  • 如果您需要更多帮助,您需要发布可编译的代码,以便其他人可以对其进行测试。见How to create a Minimal, Complete, and Verifiable example
  • +1 @Catherine Coins 在现实生活中被描述为整数美分,这对计算机来说确实有效。您的代码中可能存在错误,但这并不意味着在这种情况下使用double 而不是int 意味着更少的错误。
【解决方案2】:

您的所有变量似乎都以美分表示价格(我猜一罐可乐不是 120 美元)。但是你的零钱显然是以美元为单位的。所以你可以做的是将变化乘以 100,然后将其转换为 int

这样:

int change1 = (int) (change * 100); // convert dollars to cents and cast to int

如果您需要在某个时候以美元(而不是美分)输出 change1,则必须将其转换回来:

float result = change1 / 100.0f;

【讨论】:

  • 感谢您的回答。该代码实际上就像一个魅力。再次感谢..真的很感激。
猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 2013-06-02
  • 2020-04-05
  • 2011-09-14
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多