Repository navigation
Expand file tree
/
Copy path18-Calculator.java
More file actions
46 lines (40 loc) · 1.44 KB
/
Copy path18-Calculator.java
File metadata and controls
46 lines (40 loc) · 1.44 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
import java.util.Scanner;
class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double first = scanner.nextDouble();
System.out.print("Enter an operator (+, -, *, /, %): ");
char operator = scanner.next().charAt(0);
System.out.print("Enter second number: ");
double second = scanner.nextDouble();
switch (operator) {
case '+':
System.out.println("Result: " + (first + second));
break;
case '-':
System.out.println("Result: " + (first - second));
break;
case '*':
System.out.println("Result: " + (first * second));
break;
case '/':
if (second == 0) {
System.out.println("Division by zero is not allowed.");
} else {
System.out.println("Result: " + (first / second));
}
break;
case '%':
if (second == 0) {
System.out.println("Modulus by zero is not allowed.");
} else {
System.out.println("Result: " + (first % second));
}
break;
default:
System.out.println("Invalid operator.");
}
scanner.close();
}
}