0

爪哇。

所以基本上我在'createWarehouses'数组中的返回有问题。如果我得到一个.class expected error如果我return w[]或者return w我得到一个不匹配错误:

“需要数组,但找到仓库”

对于所有调用createWarehouses. 任何帮助将不胜感激。如果您愿意,代码也在这里:

http://pastebin.com/sAeFZu7M

import javax.swing.JOptionPane;

public class WarehouseTest
{
    public static void main(String args[])
    {
        String dataArray[][] = {{ "2200", "10", "5", "5", "200", "First Street",     "Dallas", "Texas", "76000" },
                    { "5550", "12.75", "2", "3", "15", "Mitchell Drive",     "Arlington", "Texas", "76019" },
                    { "12000", "17.5", "6", "7", "0", "Jones Blvd",     "Burleson", "Texas", "76120" },
                    { "7235", "22.35", "54", "80", "30", "Smith Circle",     "Keller", "Texas", "75020" }};

        Warehouse wArray[] = new Warehouse[4];
        wArray = createWarehouses( dataArray ); //15

        JOptionPane.showMessageDialog( null, purchaseItems( wArray ));
        JOptionPane.showMessageDialog( null, printWarehouses( wArray ));
        JOptionPane.showMessageDialog( null, printWarehouseCharge( wArray ));
    }

    public static Warehouse[] createWarehouses( String dataArray[][] )
    {
        Warehouse w[] = new Warehouse[dataArray.length];

        for( int i = 0; i < w.length; i++ )
        {
            w[i] = new Warehouse( Double.parseDouble(dataArray[i][0]),        
                                  Double.parseDouble(dataArray[i][1]),
                                  Integer.parseInt(dataArray[i][2]),
                                  Integer.parseInt(dataArray[i][3]),
                                  Integer.parseInt(dataArray[i][4]),
                                  new Address( dataArray[i][5],
                                          dataArray[i][6], dataArray[i][7],     
                                          Integer.parseInt( dataArray[i][8] )));
        }   
        return w[]; // ****<--- THIS IS PROBLEM**strong text**
    }

    public static String printWarehouses( Warehouse w[] )
    {
        String printMsg = String.format("");        

        for( int i = 1; i < w.length; i++ )
        {
            printMsg += String.format( 
            "Square Foot Size %.2f Price Per Square Foot %s Televisions %d     Computers %d Tablets %d %s", 
            Double.parseDouble( w[i][0] ), Double.parseDouble( w[i][1] ), 
            Int.parseInt( w[i][2] ), Int.parseInt( w[i][3] ), Int.parseInt( w[i]    [4] ),
            w[i][5].toString );      
        }               
        return printMsg;
    }

    public static String printWarehouseCharge( Warehouse w[] )
    {
        String chgMsg = String.format("");      

        for( int i = 1; i < w.length; i++ )
        {
            chgMsg += String.format( "$%,.2f\n", w[i].calculateWarehouseCharge()     + w[i].calculateTax() );
        }
        return chgMsg;
    }

    public static String purchaseItems( Warehouse w[] )
    {
        String p[][] = 
                {
                    {"Dallas", "1", "2", "5"}, 
                    {"Arlington", "0", "0", "15"},
                    {"Burleson", "0", "0", "3"},
                    {"Keller", "10", "25", "0"},
                    {"Dallas", "5", "0", "0"},
                    {"Arlington", "0", "1", "0"},
                    {"Burleson", "2", "4", "0"},
                    {"Keller", "0", "30", "3"}
                };

        String msg = String.format("");

        for ( int i = 0; i < w.length; i++ )
        {
            if ( w[i].getAddress().getCity().equals( p[i][0] ))
            {
                msg += String.format("%,.2f\n", w[i].purchaseTelevision(     599, Integer.parseInt( p[i][1] ))
                    + w[i].purchaseComputer( 759, w[i].Integer.parseInt(     p[i][2] ))
                    + w[i].purchaseTablet( 239.99,     w[i].Integer.parseInt( p[i][3] )));
            }   
            return msg;
        }
    }
} 
4

1 回答 1

1

发生的事情是你误会了错误。您的createWarehouses()方法应该返回一个数组。当您尝试返回时w[]w[]Warehouse类型,这就是您收到错误消息“需要数组但找到仓库”的原因。

当您将w[]to更改w为正确的返回类型时,您可能会认为您收到了相同的错误消息,但实际上,该消息来自下面的另一种方法,即purchasedItems()方法。在该方法中,您试图从循环内部返回。这也是一个编译错误。请参阅底部的我的代码以进行修复。

在您的purchasedItem()中,return应该在循环之外。

您还应该确保return wcreateWarehouses().

使用purchasedItems(),您应该将整个字符串与\ns 连接,而不是尝试多次返回

public static String purchaseItems( Warehouse w[] )
{
    String p[][] = 
            {
                {"Dallas", "1", "2", "5"}, 
                {"Arlington", "0", "0", "15"},
                {"Burleson", "0", "0", "3"},
                {"Keller", "10", "25", "0"},
                {"Dallas", "5", "0", "0"},
                {"Arlington", "0", "1", "0"},
                {"Burleson", "2", "4", "0"},
                {"Keller", "0", "30", "3"}
            };

    String msg = String.format("");

    for ( int i = 0; i < w.length; i++ )
    {
        if ( w[i].getAddress().getCity().equals( p[i][0] ))
        {
            msg += String.format("%,.2f\n", w[i].purchaseTelevision(     599, Integer.parseInt( p[i][1] ))
                + w[i].purchaseComputer( 759, w[i].Integer.parseInt(     p[i][2] ))
                + w[i].purchaseTablet( 239.99,     w[i].Integer.parseInt( p[i][3] )));
        }   
        msg += "\n";
    }
    return msg;
}

编辑:梅比斯

也许在你的printWarehouse()方法中,你想要这样的东西,因为w[][]无法访问

// this is what you have
Double.parseDouble( w[i][0] )

// try this
wArray[i].getSquareFoot();  // or whatever method you use from Warehouse to 
                            // get the square foot from your warehouse class

createWarehouse()在您想要访问的方法之外的任何地方,w[i].something将其更改为wArray[i].something. wArray[]是全局变量。w[]仅在createWarehouse()方法内局部可用。记住这一点。

于 2013-11-13T21:31:51.880 回答