package compounding; import java.util.Scanner; public class compounding1_1 { public static void main(String[] args) { // TODO 自动生成的方法存根 //tip(); while (true) { menu(); System.out.printf("请输入你要选择的功能(0~7):"); int n = scanner.nextInt(); if (n >= 0 && n <= 7) { if (n == 0) break; switch (n) { case 1: Year_end_value(); break; case 2: principal(); break; case 3: danli(); break; case 4: years(); break; case 5: APY(); break; case 6: Investment(); break; case 7: Repayment(); break; case 0: n = 0; break; } } } } static Scanner scanner = new Scanner(System.in); static void menu()// 菜单 { System.out.printf(" |-----------------------------------| "); System.out.printf(" | welcome | "); System.out.printf(" |-----------------------------------| "); System.out.printf(" | 1、计算年复利终值 | "); System.out.printf(" | 2、计算本金 | "); System.out.printf(" | 3、单利计算 | "); System.out.printf(" | 4、计算年份 | "); System.out.printf(" | 5、计算年利率 | "); System.out.printf(" | 6、等额定投 | "); System.out.printf(" | 7、等额还款 | "); System.out.printf(" | 0、退出系统 | "); System.out.printf(" |-----------------------------------| "); } static void principal()// 计算本金 { int year, n; double q, F, P; System.out.printf("复利终值:"); F = scanner.nextDouble(); System.out.printf("年利率:"); q = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年复利次数:"); n = scanner.nextInt(); //P = capital_formula(F, i, N, m); P = F / Math.pow((1 + q / n), year * n); System.out.println("年复利终值为" + F + "需要本金为:" + P); } static void Year_end_value()// 计算复利终值 { int year, n; double q, F, P; System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("年利率:"); q= scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年复利次数:"); n = scanner.nextInt(); F = P * Math.pow((1 + q / n), year * n); System.out.println("复利终值:" + F); } static void danli()// 单利计算 { int year; double q, F, P; System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("年利率:"); q = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); F = P + P * year * q; System.out.println("本息和为:" + F); } static void years()// 求年份 { int year, n; double q, F, P; System.out.printf("复利终值:"); F = scanner.nextDouble(); System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("年利率:"); q = scanner.nextDouble(); System.out.printf("年复利次数:"); n = scanner.nextInt(); year = (int) (Math.log(F / P) / Math.log(1 + q / n) / n); System.out.println("从" + P + "到" + F + "需要" + year + "年"); } static void APY()// 计算年利率 { int year, n; double q, F, P; System.out.printf("复利终值:"); F = scanner.nextDouble(); System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年复利次数:"); n = scanner.nextInt(); q = n * (Math.pow(F / P, 1.0 / (year * n)) - 1); System.out.println("从" + P + "到" + F + "需要" + q); } static void Investment()// 计算等额投资 { int n; System.out.printf(" 1:按年投资 2:按月投资 "); System.out.printf("请选择你要的功能<1|2>:"); n = scanner.nextInt(); if (n == 1) { Investmentyear(); } else if (n == 2) { Investmentmonth(); } else { System.out.printf("输入有误! "); } } static void Investmentyear(){ int year; double q, final_value, P; System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年利率:"); q = scanner.nextDouble(); final_value = P * (Math.pow(1 + q, year) - 1) /q; System.out.println(year + "年后的总产值:" + final_value); } static void Investmentmonth(){ int year; double q, final_value, P; System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年利率:"); q = scanner.nextDouble(); final_value = P * 12 * (1 + q) * (Math.pow(1 + q, year) - 1) / q; System.out.println(year + "年后的总产值:" + final_value); } static void Repayment()// 等额还款 { int year; double q, F, refund; System.out.printf("贷款金额:"); F = scanner.nextDouble(); System.out.printf("存入年限:"); year = scanner.nextInt(); System.out.printf("年利率:"); q = scanner.nextDouble(); refund = F * q / (12 * (1 + q) * (Math.pow(1 + q, year) - 1)); System.out.println("贷款" + F + "每月需要还款" + refund); } }