|
发表于 2021-12-20 15:41:36
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
编写一个类,实现根据驾龄计算保险金额的方法,方法返回值是double类型,方法参数是驾驶员的年龄,实现如下需求:
1.驾龄=年龄-16,驾龄小于4年,返回保险金额1000元,否则返回600元。
2.判断年龄,如果小于16,抛出Exception类型的异常。
3.计算保险金额的方法声明异常。
4.最后编写测试类测试,使用try-catch 捕获计算保险金额方法声明的异常。
- public class test {
-
- public Double getInsuredAmount(int age) throws Exception {
- if (age < 16)
- throw new Exception("年龄小于16岁!");
- int drivingAge = age - 16;
- if (drivingAge < 4) {
- return new Double(1000);
- } else {
- return new Double(600);
- }
- }
- public static void main(String[] args) {
- try {
- new test().getInsuredAmount(18);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码
|
|