-1

我有一个关于扫描仪输入的问题,它要求计算10年的本金、利息和总额,代码是:

package random;

import java.util.Scanner;

public class Interest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System. in );

        System.out.print("The principle is ");
        double principle = input.nextDouble();

        System.out.print("The interest rate is ");
        double interestRate = input.nextDouble();

        for (int i = 1; i <= 10; i++) {

            double Interest = principle * interestRate;
            double Total = principle + Interest;
            principle = Total - Interest;
            System.out.println("Year " + i + " principle: " + "$" + Total + " " + "Interest:            " + "$" + Interest + " " +
                "Total: " + "$" + Total);
        }
    }
}

但是,当我运行它时,它显示:

       The principle is 10000.00
       The interest rate is 0.05
       Year 1 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 2 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 3 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 4 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 5 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 6 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 7 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 8 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 9 principle: $10500.0 Interest: $500.0 Total: $10500.0
       Year 10 principle: $10500.0 Interest: $500.0 Total: $10500.0

我认为问题出在我的循环中,有人可以建议吗?干杯!

4

1 回答 1

5

这个:

principle= Total-Interest;

是相同的:

principle=principle;

自从:

double Total=principle+Interest; 

所以你的计算永远不会减少原则。

您需要首先使用标准摊销公式(我假设必须已经给您)计算每月付款,然后减去利息以查看支付了多少本金。

于 2013-09-05T20:30:14.103 回答