【发布时间】:2016-04-06 09:42:59
【问题描述】:
我使用 for 循环要求用户输入要购买的商品并计算价格...然后它会询问用户是否会继续添加另一个商品...所以它又是一个循环...然后当用户停止购买...然后它会计算总价...所以我使用:
totalprice+=total;
但是当外部 for 循环将为第二个客户重复时...totalprice 的价值仍然是第一个客户的购买价值,所以它会加起来...无论如何我可以每次都将 totalprice 值恢复为 0它为第二个客户循环?
这是我的那个方法的代码
public static void makeOrder() {
for (index = 0; index < date.length; index++) {
double price = 0;
int order;
char addOrder;
String resume;
System.out.print("\nCustomer" + (index + 1));
System.out.print("\nEnter your name: ");
String name = input.nextLine();
System.out.print("Enter the date of reservation(DD/MM/YY): ");
date[index] = input.nextLine();
System.out.print("Enter your type of table(Couple/Family): ");
String table = input.nextLine();
do {
System.out.println("BERKAT RESTAURANT MENU:\n\n MEALS \n1-Beef Bolognese: RM17.00\n2-Chicken Marsala: RM 13.00\n3-Spaghetti Carbonara: RM 9.00\n4-Fillet Mignon: RM12.00");
System.out.println("\nDRINKS \n5-Strawberry Fruit Punch: RM6.00 \n6-Vanilla Smoothies: RM 7.00\n7-Sky Juice: RM 3.00");
System.out.print("\nEnter your choice of meals/drink: ");
order = input.nextInt();
System.out.print("Enter the quantity: ");
int quantity = input.nextInt();
System.out.print("Do you want to add order?(Y/N): ");
addOrder = input.next().charAt(0);
double total = calculatePrice(order, quantity);
subtotal += total;
} while (addOrder != 'N');
System.out.println("");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
System.out.printf("The total price you have to pay is: RM%6.2f ", subtotal);
System.out.println("");
System.out.println("Thank you for coming to our restaurant, Please come again!");
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
}
}// makeOrder method
每次“for”循环返回时,我都想将小计值恢复为 0.00
【问题讨论】:
-
查看更多代码会有所帮助,以便我们了解您需要什么帮助。
-
你的意思是像
totalprice = 0;? -
请附上您的代码,因为它使社区更容易提供帮助。
-
对此感到抱歉...我将问题重新编辑...
-
小计在哪里声明?您在这里处理范围规则,因此如果没有声明的位置,我们无法明确回答。
标签: java arrays for-loop repeat