JOptionPane中的总成本没有显示

问题描述:

我如何获得总成本作为选项板显示,以及其中的饮料。我被要求模拟贝贝最佳早餐的早餐订购系统。我将显示一个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 **!)更好地理解您的代码,更重要的是,它将帮助您**更好地理解您的代码,从而修复自己的错误。此外,这表明您愿意付出额外的努力让志愿者更容易帮助您,而且这种努力非常值得赞赏。 –

你的问题看起来是在每个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

谢谢,它的工作原理! –