编写类InsuranceCheck和自定义异常类AgeException。用2010年减去某人的出生年份计算其年龄。然后用年龄减去16计算其驾龄。如果驾龄少于4年的驾驶员,每年需缴纳2000元的保险费

public class AgeException extends Exception {
	public AgeException(){	
		super();
	}
	public AgeException(String msg){
		super(msg);
	}
}

public class InsuranceCheck {
	
	public static void main(String args[]){
		int money;
		try {
			money=judge(1998);
			System.out.println(money);
		} catch (AgeException e) {
			e.printStackTrace();
		}
		
	}
	
	public static int judge(int age) throws AgeException{
		int ages;
		ages=2010-age;
		if(ages<16){
			throw new AgeException("未满16周岁,不需要交保险!");
		}
		ages=ages-16;
		if(ages<4&&ages>=0){
			return 2000;
		}
		else
			return 1000;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_44517301/article/details/93089569