-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.java
More file actions
41 lines (34 loc) · 1.24 KB
/
switch.java
File metadata and controls
41 lines (34 loc) · 1.24 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
import java.util.*;
public class s {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number1: ");
int n1 = input.nextInt();
System.out.print("Enter the number2: ");
int n2 = input.nextInt();
input.nextLine(); // Consume leftover newline character
System.out.print("Choose the operand (sum, sub, mul, div): ");
String operand = input.nextLine().toLowerCase();
switch (operand) {
case "sum":
System.out.printf("The result is %d", n1 + n2);
break;
case "sub":
System.out.printf("The result is %d", n1 - n2);
break;
case "mul":
System.out.printf("The result is %d", n1 * n2);
break;
case "div":
if (n2 == 0) {
System.out.println("Cannot divide by zero");
} else {
System.out.printf("The result is %d", n1 / n2);
}
break;
default:
System.out.println("Invalid operand. Please choose sum, sub, mul, or div.");
}
input.close();
}
}