我做错了什么 - 尝试将int和double相乘

时间:2015-08-20 01:51:36

标签: java arrays

我试图将单位乘以价格,但我似乎无法做到。这是代码:

import java.math.*;

public class FrameArray
{

    private String frameInventory;
    private int[] units;
    private double[] price;

    public FrameArray( String frame, int[] unitsArray, double[] priceArray, double value )
    {
        frameInventory = frame;
        units = unitsArray;
        price = priceArray;
    }

    public void setFrameInventory( String frame )
    {
        frameInventory = frame;
    }

    public String getFrameInventory()
    {
        return frameInventory;
    }

    public double totalValue(double value)
    {
        value = totalValue(value);
        value = units * price;
    }

    public void displayMessage()
    {
        System.out.printf( "Current frame inventory\n\n");
    }

    public void processInventory()
    {
        outputInventory();
    }

    public void outputInventory()
    {
        System.out.println( "Inventory levels:\n");
        System.out.printf( "Style     Qty    Price\n\n");

        for (int frame = 0; frame < price.length; frame++) 
            System.out.printf( "Frame %2d: %3d    %5.2f\n", frame + 1, units[ frame ], price[ frame], totalValue(frame) );
    }
}

我遇到单位*价格部分的问题。我做错了什么?

1 个答案:

答案 0 :(得分:2)

你的功能陷入无休止的循环!

public double totalValue(double value)
{
    value = totalValue(value); // Calls your function again and again
    value = units * price;

}

你可能想要做这样的事情:

public double totalValue()
{
    return units * price;
}

但是unitsprice必须是double类型或任何其他合适的类型。在你的代码中它是数组。在[{1}}函数中,你的论证value对我来说似乎有点奇怪。这不是它必须返回的(总价格)吗?

编辑:totalValueunitsprice的数组,这意味着这些数组包含double类型的元素。因此,我认为你想做的事情就像,double,因此乘以return units[correspondingIndex] * price[correspondingIndex];