import java.util.Scanner;public class compounding { public static void main(String[] args) { menu(); int m = scanner.nextInt(); switch(m) { case 1: compound_calate(); break; case 2: single_calate();break; } } private static void compound_calate() { //复利计算 while (true) { System.out.println(""); menu1(); int n = scanner.nextInt(); if (n >= 0 && n <= 6) { if (n == 0) System.exit(0); switch (n) { case 1: principal(); break; case 2: compound_final(); break; case 3: time(); break; case 4: annual_interest_rate(); break; case 5: equal_investment(); break; case 6: Average_repayment(); break; } } else { System.out.println("输入超出范围,请重新输入:"); } } } static Scanner scanner = new Scanner(System.in); static void menu()// 功能介绍函数 { System.out.printf("\t\t|******************************************************|\n"); System.out.printf("\t\t| 利息计算系统 |\n"); System.out.printf("\t\t|******************************************************|\n"); System.out.printf("\t\t| 1: 复利计算 |\n"); System.out.printf("\t\t| 2: 单利计算 |\n"); System.out.printf("\t\t|******************************************************|\n"); System.out.printf("\t请选择<1~2>:"); } static void menu1()// 菜单 { System.out.printf("\t\t|******************************************************|\n"); System.out.printf("\t\t| 1: 本金计算 |\n"); System.out.printf("\t\t| 2: 终值计算 |\n"); System.out.printf("\t\t| 3: 期数计算 |\n"); System.out.printf("\t\t| 4: 利率计算 |\n"); System.out.printf("\t\t| 5: 定期计算 |\n"); System.out.printf("\t\t| 6: 等额本息还款 |\n"); System.out.printf("\t\t|******************************************************|\n"); System.out.printf("请输入你要选择的功能(1~6):"); } static double principal_formula(double S, double i, int Year)// 本金计算公式 { double P; P = S/ Math.pow((1 + i ), Year); return P; } static void principal()// 计算本金 { int Year; double i,P,S; System.out.printf("输入期待复利终值:"); S = scanner.nextDouble(); System.out.printf("年利率:"); i = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); P = principal_formula(S, i, Year); System.out.println("年复利终值为" + S + "需要本金为:" + P); } static double compound_amount_formula(double P, double i, int Year)// 复利计算公式 { double F; F = P * Math.pow((1 + i ),Year); return F; } static void compound_final()// 计算复利终值 { int Year; double i, F, P; System.out.printf("请输入本金:"); P = scanner.nextDouble(); System.out.printf("请输入年利率:"); i = scanner.nextDouble(); System.out.printf("请输入复利次数(年):"); Year = scanner.nextInt(); F = compound_amount_formula(P, i, Year); System.out.println("复利终值:" + F); } static double single_interest_formula(double P, int Year, double i)// 单利计算公式 { double F; F = P *(1+i*Year); return F; } static void single_calate()// 单利计算 { int Year; double i, F, P; System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("年利率:"); i = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); F = single_interest_formula(P, Year, i); System.out.println("本息和为:" + F); } static int years_formula(double F, double P, double i)// 根据复利终值求年份 { int year; year = (int) (Math.log(F / P) / Math.log(1 + i )); return year; } static void time()// 求年份 { int Year; double i, F, P; System.out.printf("复利终值:"); F = scanner.nextDouble(); System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("年利率:"); i = scanner.nextDouble(); Year= years_formula(F, P, i); System.out.println("从" + P + "到" + F + "需要" + Year+ "年"); } static double rate_formula(double F, double P, int Year)// 复利的利率计算公式 { double rate; rate = (Math.pow(F / P, 1.0 / Year) - 1); return rate; } static void annual_interest_rate()// 计算年利率 { int Year; double i, F, P; System.out.printf("复利终值:"); F = scanner.nextDouble(); System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); i = rate_formula(F, P, Year); System.out.println("从" + P + "到" + F + "需要" + i); } static double equal_investment_years(double P, int Year, double i)// 按照年份等额投资计算公式 { double final_value; final_value = P * (Math.pow(1 + i, Year) - 1) / i; return final_value; } static double equal_investment_months(double P, int Year, double i)// 按照月份等额投资计算公式 { double final_value; final_value = P * 12 * (1 + i) * (Math.pow(1 + i, Year) - 1) / i; return final_value; } static void equal_investment()// 计算等额投资 { int Year, n; double i, F, P; System.out.printf("\t\t1:按年投资\n\t\t2:按月投资\n"); System.out.printf("请选择你要的功能<1|2>:"); n = scanner.nextInt(); if (n == 1) { System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); System.out.printf("年利率:"); i = scanner.nextDouble(); F = equal_investment_years(P, Year, i); System.out.println(Year+ "年后的总产值:" + F); } else if (n == 2) { System.out.printf("存入本金:"); P = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); System.out.printf("月利率:"); i = scanner.nextDouble(); F = equal_investment_months(P, Year, i); System.out.println(Year + "年后的总产值:" + F); } else { System.out.printf("输入有误!\n"); } } static double equal_repayment_formula(double F, int Year,double i)// 等额还款公式 { double refund; refund = F * i / (12 * (1 + i) * (Math.pow(1 + i, Year) - 1)); return refund; } static void Average_repayment()// 等额还款 { int Year; double i, F, P; System.out.printf("贷款金额:"); F = scanner.nextDouble(); System.out.printf("存入年限:"); Year = scanner.nextInt(); System.out.printf("年利率:"); i = scanner.nextDouble(); P = equal_repayment_formula(F, Year, i); System.out.println("贷款" + F + "每月需要还款" + P); }}