package Exception;
import java.util.Scanner;
public class Calculator {
static private float result;
static void calculate(float op1,char sign,float op2) throws IllegalSignException,ArithmeticException{
switch(sign) {
case'+':
result=op1+op2;
break;
case'-':
result=op1-op2;
break;
case'x':
result=op1*op2;
break;
case'/':
if(op2==0)
throw new ArithmeticException();
result=op1/op2;
break;
default:
throw new IllegalSignException("你输入的运算符不对");
}
}
public static void main(String []args) {
float a,b;
char o;
Scanner in=new Scanner(System.in);
//a=Float.parseFloat(args[0]);
//a=in.nextFloat();
try {
//a=in.nextFloat();
//b=in.nextFloat();
a=Float.parseFloat(args[0]);
o=args[1].charAt(0);
b=Float.parseFloat(args[2]);
calculate(a,o,b);
System.out.println(a+""+o+""+b+"="+result);
}catch(NumberFormatException e) {
System.out.println("你输入的数字有可能不是合法数字"+"注意:乘号用字符x代替");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("你没有输入所需数据"+
"程序需要两个数字和一个字符代替");
}
catch(ArithmeticException e) {
System.out.println("进行除法时,第二个数字不能为0!");
}
catch(IllegalSignException e) {
System.out.println("每次输入只能输入(+,-,x,/)其中一个"+
"注意乘号用x代替");
System.out.println(e.getMessage());
}
}
}