2016-09-25 66 views
0

我如何獲得總成本作爲選項板顯示,以及其中的飲料。我被要求模擬貝貝最佳早餐的早餐訂購系統。我將顯示一個JOptionPane來提示用戶選擇早餐和飲料。然後,我提供一個提示,要求辦公室訂購這些早餐的數量(從1到5),並使用掃描儀對象閱讀他的輸入。然後在JOptionPane中輸出用戶的賬單。JOptionPane中的總成本沒有顯示

import javax.swing.*; 
import java.util.Scanner; 
import java.text.*; 
import java.util.Locale; 
public class ProjectFourA 
{ 
    private static final float bagel = 2.00f, donut = 1.50f, croissant = 3.00f; 
    private static final float latte = 1.50f, coffee = 1.25f, milk = 1.00f, tea = 0.50f; 
    private float cost,extracost; 
    public static void main(String[]args) 
    { 
     Scanner breakfast = new Scanner(System.in); 
     NumberFormat mfmt = NumberFormat.getCurrencyInstance(Locale.US); 
     int size, choice, num; 
     String ch = JOptionPane.showInputDialog("Welcome to BeBe's Best Breakfast choose a breakfast item." + "\n1 to order Bagel"+"\n2 to order Donut"+"\n3 to order Croissant"); 
    choice = Integer.parseInt(ch); 

    String nm = JOptionPane.showInputDialog("Choose one of the following beverages:" + "\n1 for Latte"+"\n2 for Coffee"+"\n3 for Milk"+"\n4 for Tea"); 
    num = Integer.parseInt(nm); 
    float cost=0.0f, extracost=0.0f; 
    float price; 
    String str="", topstr=""; 
    if (choice == 1) 
    { 
    cost = bagel; 
    str = "Bagel"; 
    String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5"); 
    inputStr = breakfast.nextLine(); 
    } 
    else if (choice == 2) 
    { 
    cost = donut; 
    str = "Donut"; 
    String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5"); 
    inputStr = breakfast.nextLine(); 
    } 
    else if (choice == 3) 
    { 
    cost = croissant; 
    str = "Croissant"; 
    String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5"); 
    inputStr = breakfast.nextLine(); 
    } 
    float totCost = extracost + cost; 
    JOptionPane.showMessageDialog(null,("Breakfast ordered:" +str + "\nBeverage ordered: "+topstr + "\nTotal cost: $"+totCost)); 
    } 
} 
+0

我建議您儘量提高你的代碼,你在這裏發表的格式和一般的代碼。良好的格式,包括使用統一和一致的縮進樣式將幫助其他人(** us **!)更好地理解您的代碼,更重要的是,它將幫助您**更好地理解您的代碼,從而修復自己的錯誤。此外,這表明您願意付出額外的努力讓志願者更容易幫助您,而且這種努力非常值得讚賞。 –

回答

0

你的問題看起來是在每個if塊中的行inputStr = breakfast.nextLine();。你沒有爲此輸入任何輸入。所以它不會執行你的showMessageDialog。

刪除inputStr = breakfast.nextLine();

if (choice == 1) { 
      cost = bagel; 
      str = "Bagel"; 
      String inputStr = JOptionPane.showInputDialog(null, 
        "Enter quantity: 1-5"); 

     } else if (choice == 2) { 
      cost = donut; 
      str = "Donut"; 
      String inputStr = JOptionPane.showInputDialog(null, 
        "Enter quantity: 1-5"); 

     } else if (choice == 3) { 
      cost = croissant; 
      str = "Croissant"; 
      String inputStr = JOptionPane.showInputDialog(null, 
        "Enter quantity: 1-5"); 

     } 
     float totCost = extracost + cost; 
     JOptionPane.showMessageDialog(null, 
       ("Breakfast ordered:" + str + "\nBeverage ordered: " + topstr 
         + "\nTotal cost: $" + totCost)); 
+0

謝謝,它的工作原理! –