我正在为一个课程编写一个程序,该课程涉及输入和跟踪餐馆中每个人的总数和子总数

时间:2016-11-17 19:19:45

标签: java arrays for-loop

package restaurantMenu;

import java.util.Scanner;

public class RestaurantMenu {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String[] itemName = { " ", "Soup", "Wings", "Burger", "Chicken Sandwich", "Fries", "Pie", "Ice Cream",
                "Soft Drink", "Coffee" };
        double[] itemPrice = { 0, 2.50, .15, 4.95, 5.95, 1.99, 2.95, 2.99, 1.50, 1.00 };

        double grandTotal = 0;
        double total = 0;

        System.out.println("How many people in the party? ");
        int partyNumber = input.nextInt();

        for (int i = 0; i < partyNumber; i++) {
            System.out.println("Discount type:");
            System.out.println("--------------");
            System.out.println("1: Child");
            System.out.println("2. Teen");
            System.out.println("3: Senior");
            System.out.println("4. None of the above");
            int discount = input.nextInt();

            for (int k = 0; k < itemName.length; k++) {
                System.out.println(itemName[k] + "   ");
            }

            for (int j = 1; j <= 3; j++) {

                System.out.println("Please select menu item: ");
                int itemNum = input.nextInt();

                if (itemNum == 2) {
                    System.out.println("How many do you want? ");
                    int amount = input.nextInt();
                    itemPrice[2] = itemPrice[2] * amount;
                }

                total = total + itemPrice[itemNum];

            }

            System.out.println(total);

            if (discount == 1)
                total = total * 0;
            else if (discount == 2 || discount == 3)
                total = total * .75;
            else
                total = total + (total * .05);

            grandTotal = total;

        }
        System.out.println(grandTotal);
    }
}

所以,我想到的是,在跳出第二个for循环时总数没有重置,但我不一定知道我是否需要它。我想知道的是,为什么我无法正确跟踪总数,输出grandTotal,并为多人正确折扣所有内容。如果有人需要老师提供给我的输入数据,请告诉我。

1 个答案:

答案 0 :(得分:0)

在englobing for循环结束时,重置总计,而grandTotal必须添加总计。

grandTotal += total; total = 0;