-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCompute.java
More file actions
38 lines (35 loc) · 1 KB
/
StringCompute.java
File metadata and controls
38 lines (35 loc) · 1 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
/**
* 输入字符串a + b 计算结果
* @author Administrator
*
*/
public class StringCompute {
public static int ope(String str){
int result = 0;
String[] strs = str.split(" ");
if (strs.length==3) {
try {
int number1 = Integer.valueOf(strs[0]);
int number2 = Integer.valueOf(strs[2]);
if (number1>0 && number1<100 && number2>0 && number2<100 ) {
if (strs[1].equals("+")) {
result = number1+number2;
}else if (strs[1].equals("-")){
result=number1-number2;
}
}
} catch (Exception e) {
}
}
return result;
}
public static void main(String[] args) {
String string = "12 + 12";
int result = ope(string);
System.out.println(result);
string = "1 ++ 12";
result = ope(string);
System.out.println(result);
}
}
test