希望在这个总复利计算器上获得年度 returns

Looking to get the yearly returns on this total compound interest calculator

我是 Java 的新手,如果这不是编写此代码的 100% 正确方法,请原谅。

所以我已经弄乱这段代码大约 6 个小时了,我想不出如何解决这个问题:

我有一个复利计算器,它接受用户输入的期限长度、初始金额和 APR 变量。如果只是简单的一次性计算,我可以得到我想要的答案,但我真的希望它能告诉我一个学期每年增加的数量。例如: 如果利息按 10 年计算,我希望它显示之前每年的金额。现在我得到的只是一个静态数字 1,即无穷大。

我如何让这个程序向我显示该期限的总金额(即用户输入投资的长度),每年细分为每年显示的金额?

密码是:

import java.util.Scanner;
import java.lang.Math;

public class CompoundInterestCalculator {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        double initial; // the intial amount your loan is set for
        int term; // the number of years you want to calculate
        float yearlyrate; // interest rate
        int repeat;

        // System message to alert users to program use
        System.out.printf("Hi there! I can calculate compound interest for you at a fixed interest 
    rate and term.\n\nPlease enter numbers without commas or symbols to get an accurate result!");

        // Prompt for initial investment amount
        System.out.println("\nPlease enter your initial investment.");

        // Store value of user input for initial investment
        initial = scan.nextDouble();

        // Prompt for interest percentage
        System.out.println();
        System.out.println("Please enter the annual interest percentage.");
        System.out.println();
        // Store value of user input for interest amount
        yearlyrate = scan.nextFloat();

        // Prompt for length of the term for investment
        System.out.println();
        System.out.println("Please enter the length of your investment in years.");
        // Store Value of user input for term length
        term = scan.nextInt();

        //For loop to set up calulations, repeats, and totals
        for(repeat = 0; repeat < term; repeat++) {
            System.out.println();
            System.out.println("Your investment amount after" + (repeat+1) + "years is:");
                      
            // calculation for determining compound interest at a yearly rate and over the term
            double total = Math.pow(initial * (1 + (yearlyrate/100) / 12) , 12/term);

            // Displays the total value to the user
            System.out.println("$" + total);

            // Seperation line to clean it up
            System.out.println();

            // Close the scanner
            scan.close();
        }
    } 
}

任何帮助都将不胜感激,因为我对这件事实在是力不从心。

只是对你的计算逻辑做了一个小改动:


    // calculation for determining compound interest at a yearly rate and over the term
    double total = initial * Math.pow((1 + (yearlyrate/100)) , (repeat+1));