JOptionPane 中的总成本未显示
Total Cost in JOptionPane not showing up
如何获得显示为工作选项面板的总费用,以及其中的饮料。我被要求模拟 BeBe's Best Breakfast 的早餐订购系统。我要显示一个 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));
}
}
您的问题看起来是每个 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));
如何获得显示为工作选项面板的总费用,以及其中的饮料。我被要求模拟 BeBe's Best Breakfast 的早餐订购系统。我要显示一个 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));
}
}
您的问题看起来是每个 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));