자바 어떻게 가감 승제 실현
1853 단어 초급 편 자바
//package ;
import java.util.Scanner;
/* * *
*
: 1+2 * 3
subString : API */
public class Work02 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(" ;");
String str = scan.next();
String str1 = "[0-9]+";
String str2 = "[\\+\\*/-]";
String[] arr = str.split(str2);
for (int i = 0; i < arr.length; i++)
if (!arr[i].matches(str1)) {
System.out.println(" ");
return;
}
if (str.indexOf("+") > 0) Change(str.indexOf("+"), str, 1);
if (str.indexOf("-") > 0) Change(str.indexOf("-"), str, 2);
if (str.indexOf("/") > 0) Change(str.indexOf("/"), str, 3);
if (str.indexOf("*") > 0) Change(str.indexOf("*"), str, 4);
}
public static void Change(int m, String str3, int n) {
int a = Integer.parseInt(str3.substring(0, m));
int b = Integer.parseInt(str3.substring(m + 1));
int result = 0;
switch (n) {
case 1:
result = a + b;
System.out.println(" " + result);
break;
case 2:
result = a - b;
System.out.println(" " + result);
break;
case 3:
result = a / b;
System.out.println(" " + result);
break;
case 4:
result = a * b;
System.out.println(" " + result);
break;
}
}
}