-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculator.java
More file actions
53 lines (47 loc) · 1.83 KB
/
SimpleCalculator.java
File metadata and controls
53 lines (47 loc) · 1.83 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
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean cont = true;
while (cont) {
System.out.println("**SIMPLE CALCULATOR**");
System.out.print("Enter number1: ");
double n1 = sc.nextDouble();
System.out.print("Enter number2: ");
double n2 = sc.nextDouble();
System.out.print("Enter operation (+, -, *, /, %): ");
char choice = sc.next().charAt(0);
switch (choice) {
case '+':
System.out.println(n1 + " + " + n2 + " = " + (n1 + n2));
break;
case '-':
System.out.println(n1 + " - " + n2 + " = " + (n1 - n2));
break;
case '*':
System.out.println(n1 + " * " + n2 + " = " + (n1 * n2));
break;
case '/':
if (n2 != 0) {
System.out.println(n1 + " / " + n2 + " = " + (n1 / n2));
} else {
System.out.println("Error! Division by Zero!");
}
break;
case '%':
System.out.println(n1 + " % " + n2 + " = " + (n1 % n2));
break;
default:
System.out.println("Invalid operation. Please enter +, -, *, /, or %.");
}
sc.nextLine();
System.out.print("Do you want to continue (yes/no): ");
String input = sc.next().trim().toLowerCase();
if (input.equals("no") || input.equals("n")) {
cont = false;
System.out.println("Exiting...");
}
}
sc.close();
}
}