KnowledgeBoat Logo

Computer Applications

Using the switch statement in Java, write a program to display the colour of the spectrum (VIBGYOR) according to the user's choice.

Java

Java Conditional Stmts

101 Likes

Answer

import java.util.Scanner;

public class KboatSpectrum
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("VIBGYOR Spectrum");
        System.out.println("Enter your colour choice: ");
        char choice = in.next().charAt(0);
        switch (choice) {
            case 'V':
                System.out.println("Violet");
                break;
                
            case 'I':
                System.out.println("Indigo");
                break;
                
            case 'B':
                System.out.println("Blue");
                break;
                
           case 'G':
                System.out.println("Green");
                break;
                
           case 'Y':
                System.out.println("Yellow");
                break;
                
           case 'O':
                System.out.println("Orange");
                break;
                
           case 'R':
                System.out.println("Red");
                break;
                
           default:
                System.out.println("Incorrect choice");
        }
    }
}

Output

BlueJ output of Using the switch statement in Java, write a program to display the colour of the spectrum (VIBGYOR) according to the user's choice.

Answered By

49 Likes


Related Questions