-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitchExample.java
41 lines (34 loc) · 1.3 KB
/
SwitchExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class SwitchExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Select an option");
System.out.println("1. Print Message");
System.out.println("2. Perform Calculation");
System.out.println("3. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("You have selected Print message!");
System.out.println("Enter the message:");
scanner.nextLine();
String message = scanner.nextLine();
System.out.println("Message:" + message);
break;
case 2:
System.out.println("You have selected to perform calculation!");
System.out.println("Enter two numbers");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("Sum:" + (num1 + num2));
break;
case 3:
System.out.println("Exit Program");
break;
default:
System.out.println("Invalid Option!Please select valid option");
break;
}
scanner.close();
}
}