You are on page 1of 2

Java Switch statement exercises

Exercise 1: Key press valida�on


Use a switch statement to validate key-pressed on a keyboard. If the user pressed a number
between 0 and 9, the program display "Number valid", otherwise it should display "Not allowed".

Exercise 2: What month is it?


Create a Java applica�on that will prompt the user to type in any month number (1-12). Implement a
switch statement that will determine the value that the user entered and display the corresponding
month name.
As an example, if the user types in 2, the program must display February and so on. If the user types
in any other value not within 1-12, then display “Sorry, not a month number!”.

Exercise 3: Seasons
Create a Java applica�on which will prompt the user to type in any month number (1-12). Code a
switch statement that will display the appropriate season.
The seasons according to the months are:
• Summer: November, December, January and February.
• Autumn: March, April and May.
• Winter: June, July, August.
• Spring: September and October.

Exercise 4: Mul�ple choice


Write a Java program that allows the user to choose the correct answer to a given ques�on. Create
your own ques�on and possible answers. It does not have to be a JAVA-related ques�on.

An example of a possible ques�on:

What is the correct way to declare a variable to store an integer value in Java?
a. int 1x = 10;
b. int x = 10;
c. float x = 10.0f;
d. string x = "10";
Enter your choice:
------------------------------------------------------------------------------------------------
Possible responses for each choice:
a. Variable iden�fier cannot start with a number.
b. Correct. Well done!!!
c. Incorrect type altogether.
d. string is not an int. String must also start with a upper case S.

Anything else: Wrong choice. Bye bye!

You might also like