Java ProgramsControl FlowRestaurant Menu Using Switch

Restaurant Menu Using Switch in Java

beginner·  Control Flow  ·  Switch Statement

Problem

A switch statement can map a numbered menu choice directly to that item's name and price, both stored together as a single string.

Given a numbered menu choice, print the corresponding item's name and price.

Input
choice = 2
Output
Pizza - $8.49

Java Program

Java
public class RestaurantMenuSwitch { public static void main(String[] args) { int choice = 2; String item; switch (choice) { case 1: item = "Burger - $5.99"; break; case 2: item = "Pizza - $8.49"; break; case 3: item = "Salad - $4.25"; break; case 4: item = "Soda - $1.99"; break; default: item = "Invalid choice"; // required so item is always assigned } System.out.println(item); } }

Output

Pizza - $8.49

Core Logic

Matching the numbered choice against each case picks out that item's name-and-price string directly.

How It Works
  1. 1switch (choice) matches the menu number against each case label.
  2. 2Case 1 resolves to "Burger - $5.99", case 2 to "Pizza - $8.49", case 3 to "Salad - $4.25", and case 4 to "Soda - $1.99" — a small illustrative menu.
  3. 3The default case handles any number outside 1-4, printing "Invalid choice".
For choice = 2, the case 2 branch matches directly and prints "Pizza - $8.49".
💡

Key Point: Storing the name and price together as one string keeps this example simple — a real order system would likely separate them into their own fields for further calculation.

Key Concepts

switch statementint switchdefault case

Related Programs