-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOCalculator.java
More file actions
43 lines (34 loc) · 1013 Bytes
/
Copy pathOCalculator.java
File metadata and controls
43 lines (34 loc) · 1013 Bytes
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
import java.util.Scanner;
public class OCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String exp = sc.next();
int index = 0;
char op = ' ';
for (int i = 0; i < exp.length(); i++) {
char ch = exp.charAt(i);
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
op = ch;
index = i;
break;
}
}
int a = Integer.parseInt(exp.substring(0, index));
int b = Integer.parseInt(exp.substring(index + 1));
switch (op) {
case '+':
System.out.println(a + b);
break;
case '-':
System.out.println(a - b);
break;
case '*':
System.out.println(a * b);
break;
case '/':
System.out.println(a / b);
break;
}
sc.close();
}
}