0% found this document useful (0 votes)
29 views3 pages

Using The Switch Statement in Java

The document provides a Java program that uses a switch statement to display the color of the spectrum based on the user's input from the letters VIBGYOR. The program prompts the user to enter a character, converts it to uppercase, and then matches it to the corresponding color. If the input is invalid, it informs the user to enter a valid letter from VIBGYOR.

Uploaded by

venomtricks2906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Using The Switch Statement in Java

The document provides a Java program that uses a switch statement to display the color of the spectrum based on the user's input from the letters VIBGYOR. The program prompts the user to enter a character, converts it to uppercase, and then matches it to the corresponding color. If the input is invalid, it informs the user to enter a valid letter from VIBGYOR.

Uploaded by

venomtricks2906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Using the switch statement in Java, write a program to display the colour of the spectrum

(VIBGYOR) according to the user's choice. The user is expected to enter any of the alphabet of the
word VIBGYOR and the program output must display the respective colour starting with the alphabet
entered by the user.

import java.util.Scanner;

public class SpectrumColor {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Ask user for input

System.out.print("Enter a character from VIBGYOR: ");

char choice = sc.next().toUpperCase().charAt(0); // Convert to uppercase for uniformity

// Use switch statement to determine the color

switch (choice) {

case 'V':

System.out.println("V - Violet");

break;

case 'I':

System.out.println("I - Indigo");

break;

case 'B':

System.out.println("B - Blue");

break;

case 'G':

System.out.println("G - Green");

break;
case 'Y':

System.out.println("Y - Yellow");

break;

case 'O':

System.out.println("O - Orange");

break;

case 'R':

System.out.println("R - Red");

break;

default:

System.out.println("Invalid input! Please enter a letter from VIBGYOR.");

sc.close();

You might also like