日期:2014-05-20  浏览次数:20791 次

异常处理
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 则抛出IllegalargumenException异常

 rreturn (a+b+c/3);
}

------解决方案--------------------
就是几个判断啊

Java code
int average (int a,int b,int c) throws RuntimeException{ 
   if (a<0 || b<0 || c<0)
      throw new ArithmeticException();
   else if(a>100 || b>100 || c>100)
      throw new new IllegalargumenException();

   return (a + b + c) / 3;
}