天天看點

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 >= 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("\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((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("\t\t1:按年投資\n\t\t2:按月投資\n");
    System.out.printf("請選擇你要的功能<1|2>:");
    n = scanner.nextInt();
    if (n == 1) {
        Investmentyear();

    } else if (n == 2) {
        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(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);
}

}