如何将带有产品的字符串数组设置为带有价格的数组?

时间:2018-11-03 21:00:41

标签: java arrays

问题是“在SandwichCounter类中编写一个程序来计算 在食品摊位出售的三明治的成本。三种 三明治(奶酪牛排,鸡肉和沙拉)的成本, 每个三明治分别为8.5美元,7.5美元和7.0美元。创建一个 包含这些三明治名称的字符串数组。 创建另一个包含每个对应成本的数组 三明治。您的程序应读取三明治名称, 客户所需的数量。将三明治放在 命名数组并使用该索引查找每次购买的费用 成本数组。计算并打印销售的总成本”,我认为我应该将两个阵列设置为彼此相等,以便色拉= 7.0,但我无法弄清楚。谢谢!

1 个答案:

答案 0 :(得分:1)

您的课程将类似于以下内容:

public class SandwichCounter {
    private String[] sandwiches;
    private double[] prices;

    public void SandwichCounter() {
        sandwiches = {"Cheese Steak", "Chicken", "Salad"};
        prices = {8.50, 7.50, 7.00};
    }

    public void placeOrder(String sandwichName, int quantity) {
        // Boolean variable which will determine if the sandwich name is in our array (list of sandwiches).
        boolean orderPlaced = false;

        // Loop through the names of the sandwiches.
        for (int i = 0; i < sandwiches.length; i++) {
            // If the name is in the array
            if (sandwiches[i].equals(sandwichName)) {
                // Compute the cost of the sandwich.
                double cost = prices[i] * quantity;

                // Print the cost of the sandwich(es) ordered.
                System.out.println("The cost of " + quantity + " " + sandwichName + " sandwich(es) is: $" + cost + ".");

                orderPlaced = true;
                break;
            }
        }

        // The name of the sandwich is not in the array.
        if (orderPlaced == false) {
            System.out.println("We have no sandwich by the name " + sandwichName + ".");
        }
    }
}
相关问题