mercredi 17 juin 2015

Sum up int variables in Switch

please help. How can I get the total price if I'll try to purchase all the items inside the Switch. If I press x, it will ask the amount but it has wrong result when change is to be compute

import java.util.Scanner;
public class NewClass {
   public static void main(String[] args) {

  Scanner ff = new Scanner(System.in);
  String combo;
  String order = "";
  int price = 0;
   int amount = 0;
  int change = 0;
  System.out.println("Enter your order");
  loop: while (ff.hasNextLine()){
      combo = ff.next();
      switch (combo){
          case "x": break loop;
          case "a": price = 45;
              System.out.println("Burger: P45");
           break;
          case "b": price = 109;
              System.out.println("Chicken: P109");
              break;
          default: System.out.println("No such combo.");     
      }

      System.out.println(order);          
  }

  System.out.println("Please enter your amount: ");
  amount = ff.nextInt();
  change = amount - price;
  if (amount > price)
     System.out.println("Your change is: " + change);
   else 
    System.out.println("Your money is not enough.");
   }
}

Aucun commentaire:

Enregistrer un commentaire