- Home
- Chapter 1 - Object Oriented Programming Concepts
- Object Oriented Programming Concepts
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 2 - Introduction to Java
- Introduction to Java
- Multiple Choice Questions
- Assignment Questions
- Chapter 3 - Values and Data Types
- Values and Data Types
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 4 - Operators in Java
- Operators in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 5 - User-Defined Methods
- User-Defined Methods
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions
- Chapter 6 - Input in Java
- Input in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 7 - Mathematical Library Methods
- Mathematical Library Methods
- Multiple Choice Questions
- Assignment Questions
- Chapter 8 - Conditional Constructs in Java
- Conditional Constructs in Java
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 9 - Iterative Constructs in Java
- Iterative Constructs in Java
- Multiple Choice Questions
- State whether the given statements are True or False
- Assignment Questions and Programs
- Chapter 10 - Nested for loops
- Nested for loops
- Assignment Questions and Programs
- Chapter 11 - Constructors
- Constructors
- Multiple Choice Questions
- Assignment Questions and Programs
- Chapter 12 - Library Classes
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 13 - Encapsulation and Inheritance
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 14 - Arrays
- Library Classes
- Multiple Choice Questions
- Assignment Questions
- Chapter 15 - String Handling
- Library Classes
- Multiple Choice Questions
- Assignment Questions
Conditional Constructs in Java
Chapter 9
Conditional Constructs in Java
Class 9 - Logix Kips ICSE Computer Applications with BlueJ
![]() Share with a Friend |
Java Programs - Program to display the colour of the spectrum (VIBGYOR) according to the user's choice using switch statement
import java.util.Scanner;
public class VIBGYOR {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Choose a number (1-7) to display a colour:");
System.out.println("1 - Violet");
System.out.println("2 - Indigo");
System.out.println("3 - Blue");
System.out.println("4 - Green");
System.out.println("5 - Yellow");
System.out.println("6 - Orange");
System.out.println("7 - Red");
System.out.print("\nEnter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Colour: Violet");
break;
case 2:
System.out.println("Colour: Indigo");
break;
case 3:
System.out.println("Colour: Blue");
break;
case 4:
System.out.println("Colour: Green");
break;
case 5:
System.out.println("Colour: Yellow");
break;
case 6:
System.out.println("Colour: Orange");
break;
case 7:
System.out.println("Colour: Red");
break;
default:
System.out.println("Invalid choice! Please select between 1 and 7.");
}
sc.close();
}
}
Output
Output 1:
SAMPLE INPUT : Enter your choice: 3 SAMPLE OUTPUT : Colour: BlueOutput 2:
SAMPLE INPUT : Enter your choice: 7 SAMPLE OUTPUT : Colour: RedOutput 3: (Invalid Choice)
SAMPLE INPUT : Enter your choice: 9 SAMPLE OUTPUT : Invalid choice! Please select between 1 and 7.
Explanation
1. User Inputint choice = sc.nextInt();
- Reads the user’s choice (1–7).
switch (choice)
- Compares the value of choice with each case.
- Each case corresponds to one colour of the VIBGYOR spectrum.
- Prevents fall-through to the next case.
- Executes when the user enters an invalid number.
