2016-02-08 11 views
-1
import java.util.Scanner; 
import java.text.DecimalFormat; 
import java.util.Locale; 
import java.text.*; 
import javax.swing.JOptionPane; 

public class Project4B { 
    private static final float bagel = 2.00f, donut = 1.50f, croissant = 3.00f; 
    private static final float latte = 1.50f, coffee = 1.25f, milk = 1f, tea = 0.50f; 
    private static int choice, choice2; 
    private static int inFlav; 
    private static String flavorString; 

    public static void main(String[] args) { 
    NumberFormat moneyformat = NumberFormat.getCurrencyInstance(Locale.US); 

    /* String flavorString int inFlav */ 

    String str1 = JOptionPane.showInputDialog(null, 
     "WELCOME TO BeBe's Best Breakfast \nCHOOSE a Breakfast item:\n1 Bagel @ " + moneyformat.format(bagel) + "\n2 Donut @ " + moneyformat.format(donut) + 
      "\n3 Croissant @ " + moneyformat.format(croissant), 
     "Input", JOptionPane.QUESTION_MESSAGE); 
    choice = Integer.parseInt(str1); 
    switch (choice == 1) { 
    case 1: 
     flavorString = ": Onion "; 
     String flavorString = JOptionPane.showInputDialog(null, "Choose a flavor for your bagel\n1 Onion\n2 Blueberry \n3 Rye", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 

    case 2: 
     flavorString = ": Blueberry "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your bagel\n1 Onion\n2 Blueberry \n3 Rye", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
    case 3: 
     flavorString = ": Rye "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your bagel\n1 Onion\n2 Blueberry \n3 Rye", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
    default: 
     flavorString = ": plain "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Onion\n2 Blueberry \n3 Rye", "Input", JOptionPane.QUESTION_MESSAGE); 
    } 

    if (choice == 2) { 
     switch (inFlav) { 
     case 1: 
     flavorString = ": Jelly "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Jelly\n2 Chocolate\n3 Sprinkles\n4 Cinnamon ", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
     case 2: 
     flavorString = ": Chocolate "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Jelly\n2 Chocolate\n3 Sprinkles\n4 Cinnamon ", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
     case 3: 
     flavorString = ": Sprinkles "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Jelly\n2 Chocolate\n3 Sprinkles\n4 Cinnamon ", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
     case 4: 
     flavorString = ": Cinnamon "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Jelly\n2 Chocolate\n3 Sprinkles\n4 Cinnamon ", "Input", JOptionPane.QUESTION_MESSAGE); 
     break; 
     default: 
     flavorString = ": plain "; 
     JOptionPane.showInputDialog(null, "Choose a flavor for your donut\n1 Jelly\n2 Chocolate\n3 Sprinkles\n4 Cinnamon ", "Input", JOptionPane.QUESTION_MESSAGE); 
     } 
    } 

    inFlav = Integer.parseInt(flavorString); 

    String str2 = JOptionPane.showInputDialog(null, 
     "CHOOSE ONE OF THE FOLLOWING BEVERAGES: \nEnter:\n1 Latte @ " + moneyformat.format(latte) + "\n2 Coffee @ " + moneyformat.format(coffee) + 
      "\n3 Milk @ " + moneyformat.format(milk) + "\n4 Tea @" + moneyformat.format(tea), 
     "Input", JOptionPane.QUESTION_MESSAGE); 
    choice2 = Integer.parseInt(str2); 

    System.out.println("How many of these items would you like<1 to 20>"); 
    Scanner office = new Scanner(System.in); 
    int office1; 
    office1 = office.nextInt(); 

    float bageltotal = (bagel * office1); 
    float donuttotal = (donut * office1); 
    float croissanttotal = (croissant * office1); 
    float lattetotal = (latte * office1); 
    float coffeetotal = (coffee * office1); 
    float milktotal = (milk * office1); 
    float teatotal = (tea * office1); 

    float cost1 = 0.0f; 

    String choicestr = ""; 
    if (choice == 1) { 
     cost1 = bagel; 
     choicestr = ("Bagel @ "); 
    } else if (choice == 2) { 
     cost1 = donut; 
     choicestr = ("Donut @ "); 
    } else if (choice == 3) { 
     cost1 = croissant; 
     choicestr = ("Croissant @ "); 

    } 

    float cost2 = 0.0f; 
    String choicestr2 = ""; 
    if (choice2 == 1) { 
     cost2 = latte; 
     choicestr2 = ("Latte @ "); 
    } else if (choice2 == 2) { 
     cost2 = coffee; 
     choicestr2 = ("Coffee @ "); 
    } else if (choice2 == 3) { 
     cost2 = milk; 
     choicestr2 = ("Milk @ "); 
    } else if (choice2 == 4) { 
     cost2 = tea; 
     choicestr2 = ("Tea @ "); 
    } 
    float totalcost = (cost1 * office1); 
    float total2cost = (cost2 * office1); 
    float lowtotal = (cost1 + cost2); 
    float completetotal = (totalcost + total2cost); 

    JOptionPane.showMessageDialog(null, 
     ("Breakfast ordered:\n" + choicestr + flavorString + moneyformat.format(cost1) + "\n\nBeverage ordered: \n" + choicestr2 + moneyformat.format(cost2) + "\n\nTotal cost: " 
      + moneyformat.format(lowtotal) + "\n\nNumber Ordered: " + office1 + "\n\nTotal Due: " + moneyformat.format(completetotal)), 
     "YOUR BILL", JOptionPane.INFORMATION_MESSAGE); 

    } 
} 

嗨所以這是一個java1的作業。我的問題是我試圖讓味道出現在我的jOption輸出YOURBILL上。但它始終默認爲我的交換機中的默認值。我看不出爲什麼。所以我想我會休息一下,看看你有沒有什麼建議。感謝它總是默認在我的開關java

+0

你一個布爾做一個開關 –

回答

1

您在這裏有一個BUG:

switch (choice ==1){ 

它應該是:

switch (choice){ 
相關問題