Im writing a program for my Java class and i need to make a receipt like output. I have to allow the user to choose from 3 items. After choosing the item, they choose the quantity, and i multiply that by the price to give them a total. The user needs to pick all 3 items, but can pick them in any order. I am trying to make a Switch/Case method to help me assign the variables to what the user has chosen. This is what i have currently:
System.out.println("Enter name of First item");
System.out.println(" 1. Gum, 2. Soda, 3. Chips");
firstItem = keyboard.nextInt( );
switch (firstItem)
{
case 1:
System.out.println("Gum");
System.out.println(gumPrice);
System.out.println("How many packs of gum would you like to purchase?");
gumBought = keyboard.nextInt();
break;
case 2:
System.out.println("Soda");
System.out.println(sodaPrice);
System.out.println("How many cups of soda would you like to purchase?");
sodaBought = keyboard.nextInt();
break;
case 3:
System.out.println("Chips");
System.out.println(chipsPrice);
System.out.println("How many bags of chips would you like to purchase?");
chipsBought = keyboard.nextInt();
break;
default:
System.out.println("Unknown Entry");
break;
}
And Then it begins the second item Switch/Case. I Need to know how to get it to return to the Main Question (Gum/Soda/Chips) if the user picks a number outside of 1-3 or picks the same number/item twice. How can i do this efficiently?
Also, can i use Strings instead of Int to allow the user to type "Gum" instead of "1"? Thanks.
Aucun commentaire:
Enregistrer un commentaire