-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
40 lines (39 loc) · 1.55 KB
/
Copy pathMain.java
File metadata and controls
40 lines (39 loc) · 1.55 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println(calc(scanner.nextLine()));
}
}
public static String calc (String input) throws Exception {
int rez = 0;
if (input.contains("+") ){
String [] numbers = input.split("[+]");
controlNumber(numbers);
rez = Integer.parseInt(numbers[0]) + Integer.parseInt(numbers[1]);
} else if (input.contains("-")){
String [] numbers = input.split("[-]");
controlNumber(numbers);
rez = Integer.parseInt(numbers[0]) - Integer.parseInt(numbers[1]);
} else if (input.contains("*")){
String [] numbers = input.split("[*]");
controlNumber(numbers);
rez = Integer.parseInt(numbers[0]) * Integer.parseInt(numbers[1]);
} else if (input.contains("/")){
String [] numbers = input.split("[/]");
controlNumber(numbers);
rez = Integer.parseInt(numbers[0]) / Integer.parseInt(numbers[1]);
} else throw new Exception();
return String.valueOf(rez);
}
static void controlNumber(String[] num) throws Exception {
if (num.length == 2) {
for (String number: num) {
if (Integer.parseInt(number) < 1 || Integer.parseInt(number) > 10 ) {
throw new Exception();
}
}
} else throw new Exception();
}
}