7-12 sdut-sel-3-考考计算机(II)
考考计算机,由你来出题。题目格式形如:A+B的四则运算题,让计算机输出结果。
输入格式:
形如A+B的算式,A和B为整数,中间是+、-、*、/ 符号之一。算式中间没有空格。
输出格式:
一个整数。
输入样例1:
在这里给出一组输入。例如:
1+2
输出样例1:
在这里给出相应的输出。例如:
3
输入样例2:
在这里给出一组输入。例如:
1-2
输出样例2:
在这里给出相应的输出。例如:
-1
输入样例3:
在这里给出一组输入。例如:
3*5
输出样例3:
在这里给出相应的输出。例如:
15
输入样例4:
在这里给出一组输入。例如:
13/5
输出样例4:
在这里给出相应的输出。例如:
2
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
String str=cin.nextLine();
int re=1;
if(str.contains("+")==true)
{
String[] strs=str.split("\\+");
re=Integer.parseInt(strs[0].toString())+Integer.parseInt(strs[1].toString());
}
if(str.contains("-"))
{
String[] strs=str.split("\\-");
re=Integer.parseInt(strs[0].toString())-Integer.parseInt(strs[1].toString());
}
if(str.contains("*"))
{
String[] strs=str.split("\\*");
re=Integer.parseInt(strs[0].toString())*Integer.parseInt(strs[1].toString());
}
if(str.contains("/"))
{
String[] strs=str.split("\\/");
re=Integer.parseInt(strs[0].toString())/Integer.parseInt(strs[1].toString());
}
System.out.println(re);
}
}