题目是: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;
}
下面是提示:
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;
}
解决方案 »
- 有一个List里面都是对象。对象有id属性,希望返回的list按照一定规则重新排序!怎么做比较效率高啊?如果不是对象是map呢?求高效率算法
- java怎么使用局域网上的sql数据库
- 关于struts的中文问题
- 字符串替换: 如何替换中文字符冒号:和[
- 求解 算法 及 程序优化 面试题
- JDBC连接远程 ODBC 数据源,改怎么写?
- 如何写判断一个字符串是否符合一个四则运算式的正则表达式
- 一个关于rmi的问题,各位老大快来阿!
- 如何通过JDBC把文件形的数据添加到Oracle!
- 如何让包含javax.swing组件的小程序在<Applet code=XXX.jar></Applet>中执行?
- JAVA该从何处学起?
- JTextArea中的add()加组件问题?
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]
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 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了。