0330复利计算(修改版)
1 package kxj; 2 import java.util.Scanner; 3 4 public class Fulijisuan { 5 public static double p,i,f ; 6 public static double n; 7 8 //计算本金 9 public static void Benjin(){ 10 //int n; 11 //float f,i,p; 12 boolean flag; 13 Scanner scanner=new Scanner(System.in); 14 System.out.println("请输入终值: "); 15 f=scanner.nextDouble(); 16 System.out.println("请输入年利率: "); 17 i=scanner.nextDouble(); 18 System.out.println("请输入年数: "); 19 n=scanner.nextInt(); 20 if(n>=0) 21 { 22 if(i>=0&&i<=1) 23 { 24 flag=true; 25 p=(float) (f*1/Math.pow(1+i, n)); 26 } 27 else 28 { 29 System.out.println("输入的年利率有错!"); 30 flag=false; 31 32 } 33 } 34 else 35 { 36 System.out.println("输入的年数有错!"); 37 flag=false; 38 39 } 40 if(flag) 41 System.out.println("本金为: "+(double)(Math.round(p*100)/100.0)); 42 43 } 44 45 //计算本息和 46 public static void Benxihe(){ 47 double sum1=0,sum2=0; 48 boolean flag; 49 Scanner scanner=new Scanner(System.in); 50 System.out.println("请输入本金: "); 51 p=scanner.nextDouble(); 52 System.out.println("请输入年利率: "); 53 i=scanner.nextDouble(); 54 System.out.println("请输入年数: "); 55 n=scanner.nextInt(); 56 if(n>=0) 57 { 58 if(i>=0&&i<=1) 59 { 60 sum1=(float) (p*Math.pow(1+i, n)); 61 sum2=p*(1+i*n); 62 flag=true; 63 64 } 65 else 66 { 67 System.out.println("输入的年利率有错!"); 68 flag=false; 69 70 } 71 } 72 else 73 { 74 System.out.println("输入的年数有错!"); 75 flag=false; 76 77 } 78 if(flag) 79 { 80 System.out.println("复利的本息和为: "+(double)(Math.round(sum1*100)/100.0)); 81 System.out.println("单利的本息和为: "+(double)(Math.round(sum2*100)/100.0)); 82 } 83 } 84 85 //计算年数 86 public static void Nianshu(){ 87 boolean flag; 88 Scanner scanner=new Scanner(System.in); 89 System.out.println("请输入本金: "); 90 p=scanner.nextDouble(); 91 System.out.println("请输入终值: "); 92 f=scanner.nextDouble(); 93 System.out.println("请输入年利率: "); 94 i=scanner.nextDouble(); 95 if(i>=0&&i<=1) 96 { 97 n=Logarithm.log(f/p,1+i); 98 flag=true; 99 } 100 else 101 { 102 System.out.println("输入的年利率有错!"); 103 flag=false; 104 105 } 106 if(flag) 107 System.out.println("需要存的年数为: "+Math.ceil(n)); 108 } 109 110 //计算年利率 111 public static void Lilv(){ 112 boolean flag; 113 Scanner scanner=new Scanner(System.in); 114 System.out.println("请输入本金: "); 115 p=scanner.nextDouble(); 116 System.out.println("请输入终值: "); 117 f=scanner.nextDouble(); 118 System.out.println("请输入年数: "); 119 n=scanner.nextInt(); 120 if(n>=0) 121 { 122 i=Math.pow(f/p, 1.0/n)-1; 123 flag=true; 124 } 125 else 126 { 127 System.out.println("输入的年数有错!"); 128 flag=false; 129 130 } 131 if(flag) 132 System.out.println("年报酬率为: "+(double)(Math.round(i*1000)/1000.0)); 133 } 134 135 //计算本利之和连同年金投资后的总资产 136 public static void Nianjin(){ 137 boolean flag; 138 Scanner scanner=new Scanner(System.in); 139 System.out.println("请输入每年定投资金: "); 140 p=scanner.nextDouble(); 141 System.out.println("请输入年利率: "); 142 i=scanner.nextDouble(); 143 System.out.println("请输入年数: "); 144 n=scanner.nextInt(); 145 if(n>=0) 146 { 147 if(i>=0&&i<=1) 148 { 149 f=p*(1+i)*(Math.pow(1+i,n)-1)/i; 150 flag=true; 151 152 } 153 else 154 { 155 System.out.println("输入的年利率有错!"); 156 flag=false; 157 158 } 159 } 160 else 161 { 162 System.out.println("输入的年数有错!"); 163 flag=false; 164 165 } 166 if(flag) 167 System.out.println("年资产总值为:"+(double)(Math.round(f*100)/100.0)); 168 } 169 170 //计算每月等额本息还款 171 public static void BenxiHuankuan(){ 172 double f,i,p = 0; 173 int n; 174 boolean flag; 175 Scanner scanner=new Scanner(System.in); 176 System.out.println("请输入贷款金额: "); 177 f=scanner.nextDouble(); 178 System.out.println("请输入年利率: "); 179 i=scanner.nextDouble(); 180 System.out.println("请输入贷款年数: "); 181 n=scanner.nextInt(); 182 if(n>=0) 183 { 184 if(i>=0&&i<=1) 185 { 186 i=i/12; 187 n=n*12; 188 p=f*i*Math.pow(1+i, n)/(Math.pow(1+i, n)-1); 189 flag=true; 190 191 } 192 else 193 { 194 System.out.println("输入的年利率有错!"); 195 flag=false; 196 197 } 198 } 199 else 200 { 201 System.out.println("输入的年数有错!"); 202 flag=false; 203 204 } 205 if(flag) 206 System.out.println("每月等额本息还款为:"+(double)(Math.round(p*10000)/10000.0)); 207 208 } 209 210 public static void main(String[] args) { 211 int choice; 212 while(true){ 213 System.out.println("\t\t|***********************|"); 214 System.out.println("\t\t| 1. 求 本 金 \t|"); 215 System.out.println("\t\t| 2. 求 本 息 和 \t|"); 216 System.out.println("\t\t| 3. 求 年 数 \t|"); 217 System.out.println("\t\t| 4. 求 利 率 \t|"); 218 System.out.println("\t\t| 5. 求年资产总值\t|"); 219 System.out.println("\t\t| 6. 求等额本息还款\t|"); 220 System.out.println("\t\t| 7. 退 出 \t|"); 221 System.out.println("\t\t|***********************|"); 222 Scanner scanner=new Scanner(System.in); 223 System.out.println("\n请输入你的选择(1~7): "); 224 choice=scanner.nextInt(); 225 switch(choice){ 226 case 1: 227 Benjin(); 228 break; 229 case 2: 230 Benxihe(); 231 break; 232 case 3: 233 Nianshu(); 234 break; 235 case 4: 236 Lilv(); 237 break; 238 case 5: 239 Nianjin(); 240 break; 241 case 6: 242 BenxiHuankuan(); 243 break; 244 case 7: 245 System.out.println("Thanks for using!"); 246 System.exit(0); 247 break; 248 default: 249 { 250 System.out.println("输入有误!"); 251 break; 252 } 253 } 254 } 255 } 256 }