它总是在我的switch java中默认

时间:2016-02-08 01:03:04

标签: java

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上。但它始终默认为我的开关中的默认值。我无法弄清楚为什么。所以我想我会休息一下,看看你是否有任何建议。感谢

1 个答案:

答案 0 :(得分:1)

你有一个错误:

switch (choice ==1){

应该是:

switch (choice){