KnowledgeBoat Logo

Computer Applications

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

Java

Java Conditional Stmts

10 Likes

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 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

Answered By

1 Like


Related Questions