JOptionPane中的总成本没有显示出来

时间:2016-09-25 19:24:05

标签: java java.util.scanner joptionpane

如何将总成本显示为joptionpane,以及其中的饮料。我被要求为BeBe的最佳早餐模拟早餐订购系统。我将显示一个JOptionPane,以提示用户选择早餐和订购饮料。然后,我将提供办公室订购的这些早餐数量的提示(从1到5)并使用Scanner对象读取他的输入。然后在JOptionPane中输出用户的Bill。

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

1 个答案:

答案 0 :(得分: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));