java - 未正确添加客户总数

标签 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);
    }

 }

以下是我得到的结果:

Enter the number of people in the party

1

Enter 1 if customer recieves teen or eldery discount

Enter 2 if the customer recieves no discount

Enter 3 if the customer is under 5

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

Enter the corresponding number

Enter customers item

0

Enter customers item

0

Enter customers item

0

*The total for the customer is $2.6*3

The total is $2.63

最佳答案

您的问题在于

double choicePrice = 0 , customerTotalBeforeDiscount = 0;

因为它会在每次迭代时重置 customerTotalBeforeDiscount。相反,请执行以下操作:

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

关于java - 未正确添加客户总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564394/

相关文章:

Java找不到父包中的类

java - 当 session 超时时如何显示错误消息 glassfish 安全性?

JavaFX 客户端和 JDBC 服务器 : Are JavaFX Bean-style objects required on the server side?

java - RMS 存储持久数据的问题

javascript - 在javascript中定义数组并将数组的名称保留为名称

c++ - 在 C++ 中使用静态变量访问数组位置

java - JetS3t更新内容\mime类型

arrays - 在 Julia 中切片数组

arrays - 如何在 PostgreSQL 中获取数组的最后一个元素?

javascript - 寻找与当前正在进行的组合最接近的组合