2016-09-25 10 views
0

joptpaneとその中の飲み物として表示するにはどうすればよいですか? BeBe's Best Breakfastの朝食注文システムをシミュレートするように頼まれました。私はJOptionPaneを表示して、ユーザーに朝食の選択肢と注文する飲み物を促します。私はその後、オフィスから発注されるこれらの朝食の数を(1 - 5から)尋ねて、Scannerオブジェクトを使用して自分の入力を読み上げます。そして、ユーザーのBillを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

ありがとう、それは動作します!!!! –

関連する問題