所以我是一個(主要)新手到Java,我正在寫一個簡單的程序,打印出每月貸款支付表。我已經格式化了,但嵌套for循環計算已經超過了我的頭。我需要能夠扣除每月50美元的付款,並且按照用戶從1000美元的初始貸款中提示的利率計算。問題嵌套for循環計算
到目前爲止我嘗試過的所有事情都要麼導致無限循環,要麼打印所有12個月的首次餘額計算。
這可能是一個非常明顯的問題,但任何反饋將不勝感激! For循環對我來說不是很直觀,並且盯着這個相同的代碼已經停止了我的進度!
(.....)
//this method prompts the user to enter the annual interest rate and then prints it
//along with the initial loan, monthly payment, and a simple loan payment table for
//one year
private static void simpleLoanPaymentTable() {
Scanner CONSOLE = new Scanner(System.in);
double annualInterestRate;
double initialLoan = 1000.0;
double monthlyPayment = 50.0;
System.out.println("Please enter the annual interest rate:");
annualInterestRate = CONSOLE.nextDouble();
double percentAnnualRate = (annualInterestRate/100);
double percentMonthlyRate = (percentAnnualRate/12);
System.out.println();
System.out.println("The initial loan is $1000.0");
System.out.println("The monthly payment is $50.0");
System.out.println("The annual interest rate is " + annualInterestRate + "%");
System.out.println();
System.out.println("Simple Loan Payment Table For One Year");
System.out.println();
System.out.println(" Month Balance");
//create 12 rows for the months
for(int row = 1; row <= 12; row++) {
//calculate monthly balance
for(double i = 0; i <= initialLoan; i++) {
i = (initialLoan-monthlyPayment+(initialLoan*percentMonthlyRate));
System.out.println(" " + row + " " + i);
}
}
System.out.println();
}
很好的意見,我不知道爲什麼我認爲把一個第二個for循環將簡化代碼,當它剛剛結束了它過於複雜。把它放在紙上並作爲計算工作可以消除混淆。謝謝你的幫助。 – immoraliszt 2014-09-21 01:39:10