在做多个客户时,这段代码的数学计算并没有增加,我尝试过改变很多东西。
import java.util.Scanner;
public class dinerBill {
public static void main(String[] args) {
double taxRate = 0, customerTotalBeforeDiscount= 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 = .75;
taxRate = 1;
}
else if (discountType ==2) {
discountType = 1;
taxRate = 1.05;
}
else if (discountType ==3) {
discountType = 0;}
else {
System.out.println("invalid discount type entered");
System.exit(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;
customerTotal=+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;
}
grandTotal += customerTotal;
System.out.print("The total for the customer is $" );
System.out.printf("%.2f \n" , 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
4
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
1
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
2
Enter customers item
8
The total for the customer is $6.34
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
1
How many wings ordered?
5
Enter customers item
2
Enter customers item
8
The total for the customer is $15.91
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
2
Enter customers item
7
The total for the customer is $25.31
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
3
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
2
Enter customers item
7
Enter customers item
6
The total for the customer is $0.00
The total is $47.55
答案 0 :(得分:0)
for (int choices = 3; choices > 0; choices--) {
double choicePrice = 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];
customerTotal += choicePrice * (discountType * taxRate);
}
grandTotal += customerTotal;
System.out.print("The total for the customer is $");
System.out.printf("%.2f \n", customerTotal);
customerTotal = 0;
分解后,将折扣和taxType合并到客户总数中更容易。在添加到总计和打印之后,我必须确保customerTotal归零。