-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchExample2.java
More file actions
47 lines (42 loc) · 1.41 KB
/
Copy pathSwitchExample2.java
File metadata and controls
47 lines (42 loc) · 1.41 KB
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
42
43
44
45
46
47
package java_essentials;
import java.util.Scanner;
//Arithmetic operation with switch
public class SwitchExample2 {
public static void main(String[] args) {
int value1, value2;
String operation;
Scanner scan=new Scanner(System.in);
System.out.println("Enter value 1: ");
value1=scan.nextInt();
System.out.println("Enter value 2: ");
value2=scan.nextInt();
System.out.println("Enter operation to be performed: ");
System.out.println("Note: Valid Operation are +,-,/,* ");
operation= scan.next();
System.out.println("Performing Calculation ");
float result=performOperation(value1,value2,operation);
System.out.println("Result = "+result);
}
private static float performOperation(int value1, int value2, String operation) {
return switch (operation) {
case "+"->{
System.out.println(value1+"+"+value2+" is ");
yield value1+value2;
}
case "-"->{
System.out.println(value1+"-"+value2+" is ");
yield value1-value2;
}
case "*"->{
System.out.println(value1+"*"+value2+" is ");
yield value1*value2;
}
case "/"->{
System.out.println(value1+"/"+value2+" is ");
yield value1/value2;
}
default -> throw new IllegalStateException(
"Unexpected value: " + operation+" Valid Operation are +,-,/,*");
};
}
}