【问题标题】:Java Loan Calculator nested for loop based on user input基于用户输入的嵌套 for 循环的 Java 贷款计算器
【发布时间】:2016-02-03 09:28:50
【问题描述】:

任务是根据用户输入的最小和最大贷款支付年限、贷款金额、最小和最大百分比利率以及用户给出的增量值以及利率和年数来创建一个贷款计算器。 所需的输出应如下所示:

本金:$275000.0 偿还年限:10 每月利息 费率支付


6.25 3087.7 6.75 3157.66 7.25 3228.53

本金:$275000.0 偿还年限:15 每月利息 费率支付


6.25 2357.91 6.75 2433.5 7.25 2510.37

本金:$275000.0 偿还年限:20 每月利息 费率支付


6.25 2010.05 6.75 2091.0 7.25 2173.53

请帮我修正错误。谢谢!

public static void main(String[] args){
            Scanner console = new Scanner (System.in);              

            System.out.println("This program computes monthly " + "mortgage payments.");
            System.out.print("Enter the loan amount:   "); 
            double loan = console.nextDouble(); 

            System.out.print("Enter the starting number of years to repay the loan:   ");
            int startingYears = console.nextInt(); 

            System.out.print("Enter the ending number of years to repay the loan:   ");
            int endingYears = console.nextInt();

            System.out.print("Enter the years increment between tables:   ");
            int incrementYears = console.nextInt();

            System.out.print("Enter the starting loan yearly interest rate, %:   ");
            double startingRate = console.nextDouble(); 

            System.out.print("Enter the ending loan yearly interest rate, %:    ");
            double endingRate = console.nextDouble(); 

            System.out.print("Enter the increment interest rate, %:   ");
            double incrementRate = console.nextDouble();            
            System.out.println();               

            System.out.println("Principle: $" + (double) loan);
            System.out.printf("Years to repay: %d\n", startingYears);
            System.out.println("-------- -------");

            double payment;             
                System.out.println("Rate\tPayment");

            for (int j = startingYears; j <= endingYears; incrementYears++) {
            for (double i = startingRate; i <= endingRate; incrementRate++){



                int n = 12 * startingYears;
                    double c = startingRate / 12.0 / 100.0;
                payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);  

                System.out.println(i + " " + payment);
//              System.out.println(round2(startingRate) + "\t" + round2(payment));
                startingYears += incrementYears;
            }
            }


        }

        }

【问题讨论】:

  • 如何增加两个计数器?

标签: java loops for-loop nested calculator


【解决方案1】:

for (int j = startingYears; j

for (double i = startingRate; i

您永远不会增加计数器的值,即ij,因此ij 的值将始终等于初始化值,并且这些循环将运行无限次。

增加两个计数器以达到终止条件,即分别为j &lt;= endingYearsi &lt;= endingRate

这里是sn-p的代码:

System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
    System.out.printf("Years to repay: %d\n", j);
    System.out.println("-------- -------");
    System.out.println("Rate\tPayment");
    for (double i = startingRate; i <= endingRate; i+=incrementRate){
        int n = 12 * j;
        double c = i / 12.0 / 100.0;
        double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1); 
        System.out.println(i + "\t" + payment);
    }
    System.out.println();
}

【讨论】:

  • 我不是 100% 理解这个建议。我需要添加 i += incrementYears 吗?
  • for (int j = startingYears; j
  • 更新了解决方案。用上面的 sn-p 替换解决方案中 System.out.println("Principle: $" + (double) loan); 之后的所有内容。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多