-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorLoop.java
More file actions
54 lines (44 loc) · 1.6 KB
/
Copy pathCalculatorLoop.java
File metadata and controls
54 lines (44 loc) · 1.6 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
48
49
50
51
52
53
54
import java.io.*;
import java.util.Scanner;
public class CalculatorLoop{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char choice;
do {
System.out.println("Enter first number:");
double num1 = sc.nextDouble();
System.out.println("Enter second number:");
double num2 = sc.nextDouble();
System.out.println("Choose operation: + - * /");
char op = sc.next().charAt(0);
double result;
if (op == '+') {
result = num1 + num2;
System.out.println("Result: " + result);
}
else if (op == '-') {
result = num1 - num2;
System.out.println("Result: " + result);
}
else if (op == '*') {
result = num1 * num2;
System.out.println("Result: " + result);
}
else if (op == '/') {
if (num2 != 0) {
result = num1 / num2;
System.out.println("Result: " + result);
} else {
System.out.println("Error: Cannot divide by zero");
}
}
else {
System.out.println("Invalid operation");
}
System.out.println("Do you want to continue? (y/n)");
choice = sc.next().charAt(0);
} while (choice == 'y' || choice == 'Y');
System.out.println("Calculator closed.");
sc.close();
}
}