0
我試圖在這個利率計算器上使用遞歸。現在它會打印費率和每月付款等事情,但我希望它能夠打印每個月的付款金額和其他所有內容(而不僅僅是總額),直到用戶不必支付更多費用爲止。有沒有辦法做到這一點使用遞歸?斯卡拉:使用遞歸的利率計算器
import io.StdIn._
println("Enter the house loan ammount: ")
val loan = readInt()
println("Enter the number of years you want to take to pay off the loan: ")
val years = readInt()
println("Enter the annual interest rate")
val interestRate = readInt()
val monthlyPayment = (loan*(interestRate/12))/(1-
(1+interestRate/12)^years*12)
val totalPayment = monthlyPayment*years*12
def monthlyPayment2(monthlyPayment:Int):Unit = {
if (monthlyPayment>0){
println(monthlyPayment)
monthlyPayment2((loan*(interestRate/12))/(1-(1+interestRate/12)^years*12))
}
}
println ("The monthly payment is:"+monthlyPayment)
println("The total payment is:" + totalPayment)