试图弄清楚如何将数组对象数据传递给单独的方法

Trying to figure out how to pass array object data to a separate method

我是一名调酒师,想编写一个记录酒吧库存的程序。我不知道如何将 liquorCostliquorCount 数据传递给 main() 方法下面的 GetCostTotal() 方法。我绝对确定这是我做错的相当简单的事情,但我就是想不通。感谢任何帮助。

我的 Liquor class 是分开的,如果有必要我可以 post 但我不认为 class 给我带来了问题,它正在检索从数组输入到单独方法的数据。

package inventory;
import java.util.Scanner;

public class Inventory {

    public static void main(String[] args)  {
        System.out.println("How many bottles are you taking inventory of?: ");
        Scanner keyboard = new Scanner(System.in);
        int size = keyboard.nextInt();
        Liquor[] inv = new Liquor[size];
        
        for (int i = 0; i < inv.length; i++)  {
            inv[i] = new Liquor();
            System.out.println("Enter product name: ");
            inv[i].setLiquorName(keyboard.next());
            System.out.println("Enter the count for the product: ");
            inv[i].setLiquorCount(keyboard.nextDouble());
            System.out.println("Enter the cost for the product: ");
            inv[i].setLiquorCost(keyboard.nextDouble());
        }
        
        System.out.println("The sitting inventory cost of these products is: ");
        //double totalCost = 0
        for (Liquor inv1 : inv) {
            System.out.println(inv1.getLiquorName() + ": $" + inv1.getLiquorCost() * inv1.getLiquorCount());
        }
        
        double costTotal = GetCostTotal(Liquor[] inv, double liquorCost, double liquorCount);        
        System.out.println("The total cost of the inventory is: " 
            + costTotal);
        
        System.exit(0);
        
    }
    
    public static double GetCostTotal(Liquor[] inv, double liquorCost, double liquorCount)  {
        double costTotal = 0;
        for ( int i = 0; i < inv.length; i++)   {  
            costTotal += (liquorCost * liquorCount);
        }
        return costTotal;
    }
}

试试这个

    public static void main(String[] args)  {
        System.out.println("How many bottles are you taking inventory of?: ");
        Scanner keyboard = new Scanner(System.in);
        int size = keyboard.nextInt();
        Liquor[] inv = new Liquor[size];
        
        for (int i = 0; i < inv.length; i++)  {
            inv[i] = new Liquor();
            System.out.println("Enter product name: ");
            inv[i].setLiquorName(keyboard.next());
            System.out.println("Enter the count for the product: ");
            inv[i].setLiquorCount(keyboard.nextDouble());
            System.out.println("Enter the cost for the product: ");
            inv[i].setLiquorCost(keyboard.nextDouble());
        }
        
        System.out.println("The sitting inventory cost of these products is: ");
        //double totalCost = 0
        for (Liquor inv1 : inv) {
            System.out.println(inv1.getLiquorName() + ": $" + inv1.getLiquorCost() * inv1.getLiquorCount());
        }
        
        double costTotal = GetCostTotal(inv);
        System.out.println("The total cost of the inventory is: " 
            + costTotal);
        
        System.exit(0);
        
    }
    
    public static double GetCostTotal(Liquor[] inv)  {
        double costTotal = 0;
        for ( int i = 0; i < inv.length; i++)   {  
            costTotal += (inv[i].getLiquorCost() * inv[i].getLiquorCount());
        }
        return costTotal;
    }

让我们了解这里出了什么问题。
看看您是如何尝试调用 GetCostTotal() 方法的。

double costTotal = GetCostTotal(Liquor[] inv, double liquorCost, double liquorCount);

这是不正确的。你调用的方法syntax/way实际上是我们定义方法时用到的。就像你一样:

public static double GetCostTotal(Liquor[] inv, double liquorCost, double liquorCount) {}

你的电话应该是这样的:

double costTotal = GetCostTotal(inv);

此处,我们仅传递 inv,因为 liquorCostliquorCount 的数据在数组 inv.[=31= 的“每个”元素中可用。 ] 现在您可以在 GetCostTotal 方法中接受此参数。在这里,当您使用 for 循环进行迭代时,您可以将所需的数据读取为 inv[i].getLiquorCost()inv[i].getLiquorCount().

我建议您可以阅读 java 中有关定义方法和调用方法的更多信息。