扫描仪输入以计算利息,本金和总额

时间:2013-09-05 20:28:23

标签: java loops

我有一个关于扫描仪输入的问题,它要求计算原则,兴趣和总计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

我认为这个问题来自我的循环,有人可以提出建议吗?干杯!

1 个答案:

答案 0 :(得分:5)

此:

principle= Total-Interest;

与:

相同
principle=principle;

自:

double Total=principle+Interest; 

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

您需要先使用标准摊销公式计算每月付款(我假设必须给您),然后减去利息,看看支付的原则是多少。