有没有办法用单词而不是数字作为输出?

时间:2014-11-02 02:09:36

标签: java

我想使用单词而不是输入数字。对于想要在菜单上订购食物的人来说。 这是我的输出:

2
Good Salad $7.00
3
Soda $2.00
5

我只想输入Good Salad并转到下一行,依此类推。我该如何解决? 这是代码的其余部分:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Project1 {

   public static void main(String[] args) {
      int choice = 0;
      boolean doneOrdering = false;
      boolean yes = false;
      String order, A, B;
      Scanner Keyboard = new Scanner(System.in);
      DecimalFormat moneyFormat = new DecimalFormat("$#,###.00");

      double num, GoodBurger = 0, GoodSalad = 0, Soda = 0, KidsMeal = 0;
      double GoodBurgers = 7.75;
      double GoodSalads = 7.00;
      double Sodas = 2.00;
      double KidsMeals = 3.00;
      double tax;
      double subtotal = 0, total;
      int C = 0;
      double tip = 0.010;
      final double salestax = 0.081;

      System.out.println("Welcome to Good Burger!");
      System.out.println("=======================");
      System.out
            .println("Place your orders and type 'Finish' when you are done");
      System.out
            .println("--------------------------------------------------------");

      System.out.println("1. GoodBurger $8.00");
      System.out.println("2. GoodSalad $ 7.00");
      System.out.println("3. Soda $2.00");
      System.out.println("4. KidsMeal $3.00");
      System.out.println("Type '5' if you want to tip. \n");
      System.out.println("What would you like?");

      while (!doneOrdering) {
         choice = Keyboard.nextInt();
         if (choice == 1) {
            System.out.println("GoodBurger $8.00");
            subtotal = subtotal + 8.00;
         } else if (choice == 2) {
            System.out.println("Good Salad $7.00");
            subtotal = subtotal += 7.00;
         } else if (choice == 3) {
            System.out.println("Soda $2.00");
            subtotal = subtotal + 2.00;
         } else if (choice == 4) {
            System.out.println("KidsMeal $3.00");
            subtotal = subtotal + 3.00;
         } else if (choice == 5) {
            doneOrdering = true;
            System.out.println("Do you want to tip?");
            A = Keyboard.next();
            if (A.equalsIgnoreCase("yes")) {
               System.out.print("What percentage would you like to tip? ");
               C = Keyboard.nextInt();
               double tiptotal = C * tip * subtotal;
               double taxtotal = subtotal * salestax;
               System.out.println("SubTotal $" + subtotal);
               System.out.println("Tax " + salestax);
               System.out.println("Tip $" + tiptotal);
               System.out.println("-------------------------");
               System.out.println(subtotal + tiptotal + taxtotal);
            }
            if (A.equalsIgnoreCase("no")) {
               double tiptotal = C * tip * subtotal;
               double taxtotal = subtotal * salestax;
               System.out.println("SubTotal $" + subtotal);
               System.out.println("Tax " + salestax);
               System.out.println(subtotal + tiptotal + taxtotal);
            }
         } else
            System.out.println("Invalid");
      }
   }
}

2 个答案:

答案 0 :(得分:0)

此示例使用HashMap记录价格:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Project1 {

    private static String userMessage() {
        StringBuffer buffer = new StringBuffer();
        String lineSeparator = System.getProperty("line.separator");

        buffer.append("Welcome to Good Burger!");
        buffer.append(lineSeparator);
        buffer.append("=======================");
        buffer.append(lineSeparator);
        buffer.append("Place your orders and type 'Finish' when you are done");
        buffer.append(lineSeparator);
        buffer.append("--------------------------------------------------------");
        buffer.append(lineSeparator);
        buffer.append("1. GoodBurger $8.00");
        buffer.append(lineSeparator);
        buffer.append("2. GoodSalad $ 7.00");
        buffer.append(lineSeparator);
        buffer.append("3. Soda $2.00");
        buffer.append(lineSeparator);
        buffer.append("4. KidsMeal $3.00");
        buffer.append(lineSeparator);
        buffer.append("Type '5' if you want to tip. \n");
        buffer.append(lineSeparator);
        buffer.append("What would you like?");
        buffer.append(lineSeparator);

        return buffer.toString();
    }

    private static Map<String, Double> getPrices() {
        Map<String, Double> prices = new HashMap<String, Double>();

        prices.put("GoodBurgers", 7.75);
        prices.put("GoodSalads", 7.00);
        prices.put("Sodas", 2.00);
        prices.put("KidsMeals", 3.00);

        return prices;
    }

    public static void main(String[] args) {
        String choice;
        boolean doneOrdering = false;
        String tipConfirmation;
        Scanner Keyboard = new Scanner(System.in);

        double subtotal = 0;
        int C = 0;
        double tip = 0.010;
        final double salestax = 0.081;

        String userMessage = userMessage();
        System.out.println(userMessage);

        Map<String, Double> prices = getPrices();

        while (!doneOrdering) {
            choice = Keyboard.next();
            if (prices.containsKey(choice)) {
                double price = prices.get(choice);
                System.out.println(choice + " " + price);
                subtotal = subtotal + price;
            } else if (choice == "Tip") {
                doneOrdering = true;
                System.out.println("Do you want to tip?");
                tipConfirmation = Keyboard.next();
                if (tipConfirmation.equalsIgnoreCase("yes")) {
                    System.out.print("What percentage would you like to tip? ");
                    C = Keyboard.nextInt();
                    double tiptotal = C * tip * subtotal;
                    double taxtotal = subtotal * salestax;
                    System.out.println("SubTotal $" + subtotal);
                    System.out.println("Tax " + salestax);
                    System.out.println("Tip $" + tiptotal);
                    System.out.println("-------------------------");
                    System.out.println(subtotal + tiptotal + taxtotal);
                }
                if (tipConfirmation.equalsIgnoreCase("no")) {
                    double tiptotal = C * tip * subtotal;
                    double taxtotal = subtotal * salestax;
                    System.out.println("SubTotal $" + subtotal);
                    System.out.println("Tax " + salestax);
                    System.out.println(subtotal + tiptotal + taxtotal);
                }
            } else
                System.out.println("Invalid");
        }
    }
}

答案 1 :(得分:0)

因此,一种方法是将要使用的字符串存储在HashMap中,然后查找它们以获取所需的其他信息。例如:

static String[] foods = { "Good Burger", "Good Salad", "Kid's Meal", "Drink" };
static double[] costs = { 8.0, 7.0, 3.0, 0.75 };
HashMap<String, FoodItem> itemLookup = new HashMap<>();
{
  for( int i = 0; i < foods.length; i++ ) {
     itemLookup.put( foods[i], new FoodItem( foods[i], costs[i] ) );
  }
}

将所有字符串放入HashMap,再添加一个新对象FoodItem,它存储处理食品所需的任何其他相关信息。

当用户输入食物名称时,只需在HashMap中查找。

     FoodItem item = itemLookup.get( s.trim() );
     if( item == null ) System.out.println("I couldn't find a "+ s );

这里我添加了一个trim()来删除任何空格,这会导致找不到字符串,因为HashMap只返回完全匹配。

这是主要想法。以下是有点粗糙,但有效(我尝试了一次),并让你知道如何把东西放在一起。

class FoodMenu {

   class FoodItem {

      public FoodItem( String name, double cost )
      {
         this.name = name;
         this.cost = cost;
      }
      String name;
      double cost;

      @Override
      public String toString()
      {
         return "FoodItem{" + "name=" + name + ", cost=" + cost + '}';
      }

   }
   static String[] foods = { "Good Burger", "Good Salad", "Kid's Meal", "Drink" };
   static double[] costs = { 8.0, 7.0, 3.0, 0.75 };
   HashMap<String, FoodItem> itemLookup = new HashMap<>();
   {
      for( int i = 0; i < foods.length; i++ ) {
         itemLookup.put( foods[i], new FoodItem( foods[i], costs[i] ) );
      }
   }

   List<FoodItem> process( Reader input ) throws IOException {
      BufferedReader bin = new BufferedReader( input );
      ArrayList<FoodItem> order = new ArrayList<>();
      for( String s; (s = bin.readLine()) != null && s.length() > 0; ) {
         FoodItem item = itemLookup.get( s.trim() );
         if( item == null ) System.out.println("I couldn't find a "+ s );
         else order.add(item);
      }
      return order;
   }
   public static void main(String[] args) throws IOException {
      System.out.println("Enter a food item name to order it.");
      System.out.println("Items available: "+ Arrays.toString( foods ) );
      System.out.println("Press enter on a blank line to finish");
      List<FoodItem> order = new FoodMenu().process( new InputStreamReader( System.in ));
      System.out.println("You ordered: "+order );
   }
}