Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未正确添加客户总数_Java_Arrays - Fatal编程技术网

Java 未正确添加客户总数

Java 未正确添加客户总数,java,arrays,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(S

因此,当我运行此程序时,它不会将客户总数加在一起。它只会持续显示作为客户总数输入的最后一个项目

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

 }
输入相应的号码

输入客户项目

0

输入客户项目

0

输入客户项目

0

*客户的总金额为2.6*3美元

总数为2.63美元

你的问题在哪里

double choicePrice = 0 , customerTotalBeforeDiscount = 0;
因为它会在每次迭代时重置CustomerTotalBeforRediscount。相反,你应该:

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

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

 customerTotalBeforeDiscount =+ choicePrice;
 customerTotalBeforeDiscount += choicePrice;

是的,你的答案是正确的,但不知怎的,我在我原来的帖子中正确的时候,把+=改了。

为什么
choices
改为3?让它更灵活是有意义的吗?是的,有意义,但是作业中说每个人都点了3个项目。在做了这些更改之后,我仍然得到了相同的答案。我试了两个人和汤,效果很好。是否确实将
customerTotalBeforeDiscount=0
置于for循环之外?
 customerTotalBeforeDiscount =+ choicePrice;
 customerTotalBeforeDiscount += choicePrice;