2014-10-03 60 views
0

我只是第一年大學生和我的項目正在創建一個訂購系統,但我堅持定價,因爲我不能做總價。當程序循環時,它計算出價格錯誤。 A1和A2是我輸入的唯一選擇,因爲這仍然是一個未完成的項目。如何在Java訂購系統中計算總價格?

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

+3

我建議在調試器中逐步調試代碼,並在執行代碼期間觀察值。我敢打賭,你會很快找到你基於錯誤值計算事物的位置:)另外,如果你給變量賦予更多的描述性名稱,比如'totalPrice'和'priceForCurrentItem'等,它會幫助你當你在錯誤的地方使用你的變量時,它更加清晰。 – Krease 2014-10-03 23:10:08

+0

我從來不知道'麥當勞'有一個新的特許經營'MgRonalds'! :p – 2014-10-03 23:24:11

回答

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); 
+0

謝謝!你真的幫助過我這個。這是我的問題,因爲yesteday .. – leimelson06 2014-10-03 23:35:30

1

讓我們來解決這一問題邏輯:

第一循環:

輸入代碼訂購:A1

現在,price1 = 30

訂購介紹:MgBurger

輸入數量:2

quantity1 = 2 
quantity2 = quantity1 + quantity2 = 2 + 0 = 2; 
price2 = 30 * 2 = 60; 

同意?

另一個命令? (Y/N)爲:y

第二環路,我們還有price1=30quantity1 = 2quantity2 = 2price2 = 60。現在:

輸入代碼訂購:A2

price1 = 139 

訂購介紹:巨無霸

輸入數量:2

quantity1 = 2; 
quantity2 = quantity1 + quantity2 = 2 + 2 = 4; 
price2 = 139 * 4 = 556; 

找到第m攝影了嗎? quantity2仍然保留循環中前一個事務的值。

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