-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgrebraicOperationsSwitchCase.java
59 lines (48 loc) · 1.36 KB
/
AlgrebraicOperationsSwitchCase.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class AlgrebraicOperationsSwitchCase {
public static void main(String args[]) {
//Declaring variables
double n1,n2,res = 0;
System.out.println("Algebraic Operations of two Numbers with Switch Case Loop\n---");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number 1: ");
n1 = scanner.nextDouble();
System.out.println("Enter the number 2: ");
n2 = scanner.nextDouble();
System.out.println("---\nEnter 1 for addition or\n2 for subtraction or\n3 for multiplication or\n4 for division");
int operator = scanner.nextInt();
//Solving by switch case loop for simpler code.
switch(operator) {
case 1:
res = n1+n2;
break;
case 2:
res = n1-n2;
break;
case 3:
res = n1*n2;
break;
case 4:
res = n1/n2;
break;
default:
System.out.println("You have chosen a wrong option.\nPlease choose from the above given options.");
end();
}
System.out.println(res);
end();
scanner.close();
}
static void end() {
System.out.println("---\nEnter Y to continue or any key to exit!");
Scanner getEndOption= new Scanner(System.in);
String option = getEndOption.next();
if(option.equalsIgnoreCase("Y")) {
main(null);
}
else {
System.out.println("The Program has ended. Please run the program again.");
}
getEndOption.close();
}
}