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

一编程题?如何触发ArithmeticException异常和IllegalArgumentException 异常
/**
*argument()方法用于求三个数的平均数,他有三个整形形式的参数,且此三个形式的参数取值范围均为【0,100】。常若形式参数的值小于0或大于100,
*常若形式参数的值小于0或大于100,程序就会抛出异常。请完成此average()方法。
*/



public class Test3{
static 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;
}
public static void main(String[] args){
try{
average(-1,2,3);
}catch(RuntimeException e){
System.out.println(e);
}
}

}(发帖)
------最佳解决方案--------------------
这样可以吗?

public class Test3
{
static int average(int a,int b,int c)throws RuntimeException
{
//a<0,b<0,c<0,则抛出ArithmeticException异常
if(a<0 
------其他解决方案--------------------
 b<0