题目是:average()方法用于求三个数的平均数,它有三个整数形式参数,且此三个形式参数的取值范围为[0,100]。倘若形式参数得到的值小于0或者大于100,程序就会抛出异常。请完成此average()方法。
下面是提示:
   int average(int a,int b,int c) throws RuntimeException{
   //a<0,b<0,c<0,则抛出ArithmeticException异常;
  //a>100,b>100,c>100,则抛出IllegalArgumentException异常
return (a+b+c)/3;
}

解决方案 »

  1.   

    [code]
    public static int average(int a,int b,int c) throws RuntimeException{
    if(a<0||b<0||c<0)
    throw new ArithmeticException();
    if(a>100||b>100||c>100)
    throw new IllegalArgumentException();
    return (int)(a+b+c)/3;
    }
    [/code]
      

  2.   

    public static int average(int a,int b,int c) throws RuntimeException{
    if(a<0||b<0||c<0)
    throw new ArithmeticException();
    if(a>100||b>100||c>100)
    throw new IllegalArgumentException();
    return (int)(a+b+c)/3;
    }
      

  3.   

    2楼答案正确。完整代码如下:
    public   class  Demo

    public   static   int   average(int   a,int   b,int   c)   throws   RuntimeException{ 
    if(a <0|| b <0|| c <0) 
    throw   new   ArithmeticException(); 
    if(a> 100 ||b> 100 ||c> 100) 
    throw   new   IllegalArgumentException(); 
    return   (int)(a+b+c)/3; 
    } public static void main(String[] args){
    try{
    int t=Demo.average(4, 6, 12);
    System.out.println(t);
    //int r=Demo.average(-3, 9, -6);System.out.println(r);
    int s=Demo.average(123, 3, 8);System.out.println(s);
    }
    catch(ArithmeticException e){System.out.println(e);}
    catch(IllegalArgumentException e){System.out.println(e);}
    catch(Exception e){System.out.println(e);}
    }
    }
    但要是a,b,c中既有小于0又有大于100的,那就只能抛出ArithmeticException了。