0330复利计算java版

时间:2023-03-10 06:40:24
0330复利计算java版
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 >= && n <= ) {
if (n == )
break;
switch (n) {
case :
Year_end_value();
break;
case :
principal();
break;
case :
danli();
break;
case :
years();
break;
case :
APY();
break;
case :
Investment();
break;
case :
Repayment();
break;
case :
n = ;
break; }
}
} }
static Scanner scanner = new Scanner(System.in);
static void menu()// 菜单
{
System.out.printf("\t\t|-----------------------------------|\n");
System.out.printf("\t\t| welcome |\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| 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| 7、等额还款 |\n");
System.out.printf("\t\t| 0、退出系统 |\n");
System.out.printf("\t\t|-----------------------------------|\n"); }
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(( + 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(( + 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( + 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)) - );
System.out.println("从" + P + "到" + F + "需要" + q);
} static void Investment()// 计算等额投资
{
int n;
System.out.printf("\t\t1:按年投资\n\t\t2:按月投资\n");
System.out.printf("请选择你要的功能<1|2>:");
n = scanner.nextInt();
if (n == ) {
Investmentyear(); } else if (n == ) {
Investmentmonth(); } else {
System.out.printf("输入有误!\n");
} }
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( + q, year) - ) /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 * * ( + q) * (Math.pow( + q, year) - ) / 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 / ( * ( + q) * (Math.pow( + q, year) - ));
System.out.println("贷款" + F + "每月需要还款" + refund);
} }