Java用args数组接收两个整数,做除法运算

img

public class Test{
  public static void main(String [] args){
    int a, b;
    float rst;
    try {
        if (args.length != 2) {
            throw new IndexOutOfBoundsException();
        }
        a = Integer.parseInt(args[0]);
        b = Integer.parseInt(args[1]);
        if (b == 0) {
            throw new ArithmeticException();
        }
        rst = (float) a / b;
        System.out.println("result = " + rst);
    } catch (IndexOutOfBoundsException e) {
        System.out.println("输入参数不够或超出");
    } catch (NumberFormatException e) {
        System.out.println("非法参数");
    } catch (ArithmeticException e) {
        System.out.println("除零异常");
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

img

img

img

img

public class Test{
  public static void main(String [] args){
    float a,b,s;
    a = Float.parseFloat(args[0]);
    b = Float.parseFloat(args[1]);
    try {
            s = a / b;
            System.out.println("s="+s);
        }catch (ZeroException e){
            System.out.println("分母不能为0");
        }catch (NumberFormatException e){
            System.out.println("非法参数");
        }catch (Exception e){
            e.printStackTrace();
        }
  }
}