ICSE Class 9 Computer Applications
Question 7 of 17
Conditional Constructs in Java — Question 7
Back to all questions 7
Question Question 7
Using the switch statement in Java, write a program to display the colour of the spectrum (VIBGYOR) according to the user's choice.
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
