0320复利计算5
7. 你写的程序能让客户随意操作吗?误输入数据、不小心做了非常规的操作程序是什么反应?
相互观赏程序运行情况。
8. 如果向银行贷款10万元,年利率6.5%,期限为10年,那么每月等额本息还款多少?(算复利条件下等额还款金额)
1 package bin; 2 3 import java.util.Scanner; 4 5 public class compounding { 6 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 Scanner s=new Scanner(System.in); 11 System.out.println("1.复利计算 2.单利计算 3.总额反向计算本金 4.买股票算年份 5.本金终值求利率 6.每年都投入本金 7.贷款"); 12 int num=s.nextInt(); 13 14 switch(num){ 15 case 1: 16 one(s); 17 break; 18 case 2: 19 two(s); 20 break; 21 case 3: 22 three(s); 23 break; 24 case 4: 25 four(s); 26 break; 27 case 5: 28 five(s); 29 break; 30 case 6: 31 System.out.println("请选择 1.按年定投 2.按月定投:"); 32 int six=s.nextInt(); 33 if(six==1) 34 six1(s); 35 else 36 six2(s); 37 break; 38 case 7: 39 seven(s); 40 break; 41 default: 42 System.out.println("输入有误"); 43 break; 44 } 45 46 } 47 48 private static void five(Scanner s) { 49 double sum=0; 50 System.out.println("请输入本金:"); 51 double startmoney=s.nextDouble(); 52 System.out.println("请输入终值:"); 53 double endmoney=s.nextDouble(); 54 System.out.println("请输入年数:"); 55 double years=s.nextInt(); 56 double r=Math.pow((endmoney/startmoney), 1/years)-1; 57 System.out.println(r); 58 } 59 60 private static void four(Scanner s) { 61 double sum=0; 62 System.out.println("请输入本金:"); 63 double startmoney=s.nextDouble(); 64 System.out.println("请输入终值:"); 65 double endmoney=s.nextDouble(); 66 System.out.println("请输入利率:"); 67 double r=s.nextDouble(); 68 double years=Math.log(endmoney/startmoney)/Math.log(1+r); 69 System.out.println("存入"+startmoney+"每年按"+r+"利率最终获得"+endmoney+"需要"+years); 70 } 71 72 private static void three(Scanner s) { 73 double sum=0; 74 System.out.println("请输入终值:"); 75 double money=s.nextDouble(); 76 System.out.println("请输入年数:"); 77 int years=s.nextInt(); 78 System.out.println("请输入利率:"); 79 double r=s.nextDouble(); 80 double Single=money/(1+years*r); 81 double Double=money/(Math.pow((1+r),years)); 82 System.out.println("单利现值为:"+Single); 83 System.out.println("复利现值为:"+Double); 84 } 85 86 private static void two(Scanner s) { 87 double sum=0; 88 System.out.println("请输入本金:"); 89 double money=s.nextDouble(); 90 System.out.println("请输入年数:"); 91 int years=s.nextInt(); 92 System.out.println("请输入利率:"); 93 double r=s.nextDouble(); 94 sum=money*r*years; 95 System.out.println("单利计算"+years+"年后总额为:"+(sum+money)); 96 } 97 98 private static void one(Scanner s) { 99 double sum=0; 100 System.out.println("请输入本金:"); 101 double money=s.nextDouble(); 102 System.out.println("请输入年数:"); 103 int years=s.nextInt(); 104 System.out.println("请输入利率:"); 105 double r=s.nextDouble(); 106 107 sum=money*Math.pow(1+r, years); 108 System.out.println(years+"年后总额为"+sum); 109 } 110 111 private static void six1(Scanner s){ 112 double sum=0; 113 System.out.println("请输入每年投入本金:"); 114 double money=s.nextDouble(); 115 System.out.println("请输入年数:"); 116 int years=s.nextInt(); 117 System.out.println("请输入利率:"); 118 double r=s.nextDouble(); 119 sum=money*(1+r)*(-1+Math.pow(1+r, years))/r; 120 121 System.out.println("年后总额为"+sum); 122 } 123 private static void six2(Scanner s){ 124 double sum=0; 125 System.out.println("请输入每月投入本金:"); 126 double money=s.nextDouble(); 127 System.out.println("请输入年数:"); 128 int years=s.nextInt(); 129 System.out.println("请输入利率:"); 130 double r=s.nextDouble(); 131 sum=money*12*(1+r)*(-1+Math.pow(1+r, years))/r; 132 System.out.println("年后总额为"+sum); 133 } 134 135 private static void seven(Scanner s) { 136 double sum=0; 137 System.out.println("请输入本金:"); 138 double money=s.nextDouble(); 139 System.out.println("请输入年数:"); 140 int years=s.nextInt(); 141 System.out.println("请输入利率:"); 142 double r=s.nextDouble(); 143 sum=money*Math.pow(1+r, years); 144 double monthMoney=sum/years/12; 145 System.out.println("每月还的本息金额为:"+monthMoney); 146 } 147 148 149 150 }