定义类:
[将两个类,放在一个包中.]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package cn.puruidong.accp_13; /* * 模拟一个简单的购房商贷 * 书本第290页 */ import java.util.Scanner; public class Mortgage { private double allmoney;//总利息 private double total ;//贷款金额 private double moonmoney ; //每月还款金额 private int year ; //贷款年限 public void show (double allmoney,double total,double interestrates,double moonmoney,int year){ this.allmoney=allmoney;//总利息 this.total=total;//贷款金额 this.moonmoney=moonmoney;//每月还款金额 this.year=year;//贷款年限 } public void newshow (){ Scanner input = new Scanner (System.in); System.out.println("请输入贷款金额:"); total = input.nextInt();//录入 System.out.println("请选择贷款年限:"); System.out.println("1,3年(36个月)\t2,5年(60个月)\t3,20年(240个月):"); year = input.nextInt();//录入 switch (year){ //使用switch来处理年限数据 case 1 : allmoney = total * 0.0603;//计算总利息 moonmoney = (total+allmoney)/36; //计算每月还款金额 System.out.println(); System.out.println("月供为:"+moonmoney); break; case 2 : allmoney = total * 0.0612; moonmoney = (total+allmoney)/60; System.out.println(); System.out.println("月供为:"+moonmoney); break; case 3 : allmoney = total * 0.0639; moonmoney = (total+allmoney)/240; System.out.println(); System.out.println("月供为:"+moonmoney); break; default : System.out.println("数字输入错误,请重新输入!"); } } } |
测试类:
1 2 3 4 5 6 7 8 9 10 11 12 | package cn.puruidong.accp_13; /* * 房贷的测试,输出 * 书本第290页,3页 */ public class TestMortgage { public static void main(String[] args) { // TODO Auto-generated method stub Mortgage center = new Mortgage (); center.newshow(); } } |