ICSE Class 10 Computer Applications Question 24 of 43

Conditional Constructs in Java — Question 24

Back to all questions
24
Question

Question 24

Using the switch statement in Java, write a program to display the name of the city according to the user's choice.
D — Delhi, M — Mumbai, K — Kolkata, C — Chennai

Answer
import java.util.Scanner;

public class KboatCityName
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter your choice: ");
        char ch = in.next().charAt(0);
        switch (ch) {
            case 'D':
            System.out.println("Delhi");
            break;
            
            case 'M':
            System.out.println("Mumbai");
            break;
            
            case 'K':
            System.out.println("Kolkata");
            break;
            
            case 'C':
            System.out.println("Chennai");
            break;
            
            default:
            System.out.println("Invalid choice");
        }
    }
}
Output
BlueJ output of KboatCityName.java