我只是大学一年级学生,我的项目正在创建一个订购系统,但我坚持定价,因为我不能正确地做总价。当程序循环时,它计算出错误的价格。 A1和A2是我输入的唯一选择,因为这仍然是一个未完成的项目。
import java.io.*;
public class Ordering_System
{
public static void main(String []args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String order,again;
int quantity,price1=0,price2=0,loop1=0,quantity1,quantity2=0;
System.out.println(" ");
System.out.println("Welcome to MgRonalds, What do you want to order?");
System.out.println(" ");
System.out.println("*******************************************************************");
System.out.println("* Code Meal ''MENU'' Price *");
System.out.println("* *");
System.out.println("* (A1) MgBurger P30.00 *");
System.out.println("* (A2) Big Mac P139.00 *");
System.out.println("* (B1) Cheese Burger P35.00 *");
System.out.println("* (B2) Chicken Burger P50.00 *");
System.out.println("* (C1) MgNuggets P65.00 *");
System.out.println("* (C2) MgChicken P79.00 *");
System.out.println("* (D1) MgSpagetti P60.00 *");
System.out.println("* (D2) MgFries P40.00 *");
System.out.println("* (E1) Coke P10.00 *");
System.out.println("* (E2) Sprite P10.00 *");
System.out.println("* (E3) Royal P10.00 *");
System.out.println("* (F1) Sundae P25.00 *");
System.out.println("* (F2) MgFloat P25.00 *");
System.out.println("* *");
System.out.println("*******************************************************************");
do{
System.out.println("");
System.out.print("Enter Code Order : ");
order=br.readLine();
if (order.equalsIgnoreCase("A1")) {
price1=30;
System.out.println("Order Description : MgBurger ");
} else if (order.equalsIgnoreCase("A2")) {
price1=139;
System.out.println("Order Description : Big Mac ");
}
System.out.print("Enter Quantity : ");
quantity1= Integer.parseInt(br.readLine());
quantity2=quantity1+quantity2;
price2=price1*quantity2;
System.out.print("Another Order? (Y/N) : ");
again=br.readLine();
if (again.equalsIgnoreCase("y"))
loop1=loop1+1;
else loop1=loop1-100;
} while (loop1==1);
System.out.println(" ");
System.out.println("Total Price : "+price2);
}
}
以下是输出示例:
Enter Code Order : a1
Order Description : MgBurger
Enter Quantity : 2
Another Order? (Y/N) : y
Enter Code Order : a2
Order Description : Big Mac
Enter Quantity : 2
Another Order? (Y/N) : n
Total Price : 556
答案应该是338而不是556
答案 0 :(得分:1)
让我们逻辑地解决这个问题:
在第一次循环:
输入代码订单:a1
现在,price1 = 30
订单描述:MgBurger
输入数量:2
quantity1 = 2
quantity2 = quantity1 + quantity2 = 2 + 0 = 2;
price2 = 30 * 2 = 60;
同意?
另一个命令? (是/否):y
在第二次循环上,我们仍然有price1=30
,quantity1 = 2
,quantity2 = 2
和price2 = 60
。现在:
输入代码订单:a2
price1 = 139
订单描述:巨无霸
输入数量:2
quantity1 = 2;
quantity2 = quantity1 + quantity2 = 2 + 2 = 4;
price2 = 139 * 4 = 556;
发现错误了吗? quantity2
仍然保留循环中前一个事务的值。
答案 1 :(得分:0)
你干脆怎么样?
添加变量total =0;
System.out.print("Enter Quantity : ");
quantity1 = Integer.parseInt(br.readLine());
// quantity2=quantity1+quantity2;
// price2=price1*quantity2;
total += price1 * quantity1;
...
System.out.println(" ");
System.out.println("Total Price : " + total);
答案 2 :(得分:0)
您可以按如下方式简化代码。还要考虑使用浮动值代替int。
public static void main(String[] args) throws Exception {
System.out.println(" ");
// Display the Menu
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String order, again;
int quantity = 0;
int total = 0;
while (true) {
System.out.println("");
System.out.print("Enter Code Order : ");
order = br.readLine();
System.out.print("Enter Quantity : ");
quantity = Integer.parseInt(br.readLine());
total += getPrice(order) * quantity;
System.out.print("Another Order? (Y/N) : ");
again = br.readLine();
if (again.equalsIgnoreCase("N")) {
break;
}
}
System.out.println(" ");
System.out.println("Total Price : " + total);
}
private static int getPrice(String order) {
int price = 0;
// using switch-case would be more appropriate
if (order.equalsIgnoreCase("A1")) {
price = 30;
System.out.println("Order Description : MgBurger ");
} else if (order.equalsIgnoreCase("A2")) {
price = 139;
System.out.println("Order Description : Big Mac ");
}
return price;
}