‘+’的结果错了,别的没错,不知道什么问题。
class SimpleCal2 {
private int operand1,operand2;
private char operator;
SimpleCal2(){}
SimpleCal2(int operand1,char operator,int operand2){
this.operand1=operand1;
this.operator=operator;
this.operand2=operand2;
}
private int cal() throws ArithmeticException,Exception{
int result=Integer.MIN_VALUE;
if(operator=='+')
result=operand1+operand2;
else if(operator=='-'){
result=operand1-operand2;
}
else if(operator=='*'){
result=operand1*operand2;
}
else if(operator=='/'){
result=operand1/operand2;
}
else {
throw new Exception("输入的运算符错误!");
}
return result;
}
public void showResult() throws ArithmeticException,Exception{
System.out.println("运算结果:"+operand1+operator+operand2+"="+cal());
}
public static void main(String[] args){
int p1,p2;
try{
p1=Integer.parseInt(args[0]);
char op=args[1].charAt(0);
p2=Integer.parseInt(args[2]);
SimpleCal2 exp=new SimpleCal2(p1,op,p2);
exp.showResult();
}
catch(NumberFormatException e1){
System.out.println("输入的运算数不是整数!");
}
catch(ArrayIndexOutOfBoundsException e2){
System.out.println("输入的运算式不完整!");
}
catch(ArithmeticException e3){
System.out.println("除数为0,不能进行除法运算!");
}
catch(Exception e4){
e4.printStackTrace();
}
}
}