没有正确添加客户总数

时间:2017-11-30 01:13:34

标签: java arrays

因此,当我运行它时,它不会将客户总数添加到一起。它只是显示输入的最后一项作为客户总数。

import java.util.Scanner;

public class dinerBill {

public static void main(String[] args) {


    double taxRate = 0, customerTotal=0, discountType = 0, grandTotal= 0;


    Scanner in = new Scanner(System.in);
    String [] itemName = { "0) Soup", "1) Wing", "2) Burger", "3) Chicken Sandwich", "4) Fries", "5) Pie", "6) Ice Cream", "7) Soft drink", "8) Coffee"};
    double [] itemPrice= {2.50 , .15 , 4.95, 5.95, 1.99, 2.95, 2.99, 1.50, 1.00};

    System.out.println("Enter the number of people in the party");
    int numberOfPeople  = in.nextInt();

    while (numberOfPeople >0) {

    System.out.println("Enter 1 if customer recieves teen or eldery discount ");
    System.out.println("Enter 2 if the customer recieves no discount");
    System.out.println("Enter 3 if the customer is under 5");
    discountType = in.nextInt();

    if (discountType == 1) {
        discountType = .85;
         taxRate = 1;
    }
    if (discountType ==2) {
        discountType = 1;
        taxRate = 1.05;
    }
    if (discountType ==3)
        discountType = 0;



    System.out.printf("%-24s", "Menu");
    System.out.print("Prices" + "\n");
    System.out.println("--------------------------------");
    for (int i = 0; i < itemName.length; i++) {

    System.out.printf("%-24.21s" ,itemName[i]);
    System.out.print(itemPrice[i] +"\n");

    }
    System.out.println("Enter the corresponding number");
    for (int choices=3; choices > 0; choices--) {
        double choicePrice = 0 , customerTotalBeforeDiscount = 0;

        System.out.println("Enter customers item");
        int customerItem = in.nextInt();

        if (customerItem ==1) {
        System.out.println("How many wings ordered?");
        int wingsOrdered = in.nextInt();
        double priceOfWings = wingsOrdered * itemPrice[1];
        choicePrice = priceOfWings;}
        else
            choicePrice = itemPrice[customerItem];

        customerTotalBeforeDiscount +=choicePrice;
        double customerTotalBeforeTax = customerTotalBeforeDiscount * discountType;
        customerTotal = customerTotalBeforeTax * taxRate;
    }

    System.out.print("The total for the customer is $" );
    System.out.printf("%.2f \n" , customerTotal );

    grandTotal += customerTotal;


    numberOfPeople--;
    }

    System.out.print("The total is $");
    System.out.printf("%.2f", grandTotal);

    in.close();

    System.exit(0);
    }

 }

以下是我得到的结果:

  

输入聚会中的人数

1

  

如果客户收到青少年或老年人折扣,请输入1

     

如果客户没有收到折扣,请输入2

     

如果客户不满5

,请输入3

2

Menu                    Prices
--------------------------------
0) Soup                 2.5
1) Wing                 0.15
2) Burger               4.95
3) Chicken Sandwich     5.95
4) Fries                1.99
5) Pie                  2.95
6) Ice Cream            2.99
7) Soft drink           1.5
8) Coffee               1.0
  

输入相应的号码

     

输入客户商品

0

  

输入客户商品

0

  

输入客户商品

0

  

*客户总额为2.6美元* 3

     

总额为2.63美元

2 个答案:

答案 0 :(得分:2)

你的问题在

double choicePrice = 0 , customerTotalBeforeDiscount = 0;

因为它会在每次迭代时重置customerTotalBeforeDiscount。而是做:

double customerTotalBeforeDiscount = 0;
for (int choices=3; choices > 0; choices--) {
    double choicePrice = 0;

答案 1 :(得分:0)

看了之后,客户总数确实重置为0,在我修复之后,我注意到它是= +而不是+ =。

 customerTotalBeforeDiscount =+ choicePrice;
 customerTotalBeforeDiscount += choicePrice;

所以是的,你的是正确的,但不知怎的,我在原来的帖子中正确地改变了+ =。