2016-01-21 120 views
-4

這是一個程序,用戶可以輸入貸款數量和貸款期限,並顯示每個利率從5%到8%的每月和總支付,並帶有一個增量1/8。返回到循環開始java

我可能在這裏沉迷於自己,因爲我對編程非常陌生,但是我太深,想弄明白這一點。

用我目前的代碼,第一行輸出正確,顯示費率,總額和每月。然而,在那之後,代碼只是繼續輸出最內層的循環。我需要返回到循環的開始。如果您能指出我正確的方向,將不勝感激。

P.S.我知道我的填充不到位。我最關心的問題是首先將算法放在「紙張」上,然後擔心它的美麗。

package loancalc194; 

import java.util.Scanner; 

public class LoanCalc194 { 

public static void main(String[] args) { 

//LOAN CALCULATOR 
//user enters loan AMOUNT, loan PERIOD(years), 
//output displays MONTHLY and TOTAL payments 
//^displayed per 1/8 increment from 5-8% interest 

Scanner in = new Scanner(System.in); 

//prompt user 
//retrieve loan amount and period 
System.out.print("Please enter the loan amount: "); 
double amount = in.nextDouble(); 
System.out.print("Please enter the loan period (years): "); 
int period = in.nextInt(); 

//INTEREST LOOP/////////////////////////////////////////////////// 

double interest = .05000; 
double inc = 0.00125; 
interest = interest + inc; 
double monthly = 0; 
double total = 0; 

System.out.println("Interest Rate\t\t\tTotal Payment\tMonthly Payment"); 

while (interest < .08){ 
    //interest = interest + inc; 
    System.out.print(interest + "\t"); 

    while (true){ 
     total = (amount * period) + (interest * amount); 
     System.out.print("\t" + total + "\t\t"); 

     while (true) { 
      monthly = ((total/period)/12); 
      System.out.println(monthly); 
      //interest = interest + inc; 
     } 
    } 
} 
+0

你需要爲其他2個循環的休息條件。 – brso05

+2

什麼是這些循環甚至應該做的,你只是無盡地計算相同的值...? – redFIVE

+2

爲什麼你甚至把這些線條放在循環中?沒有任何意義 –

回答

0

一個循環應該足夠你想要的。

您的兩個while(true)循環除了永遠循環相同的值之外別無它物。

在下面的代碼中,興趣被遞增,並在每個循環計算,直到興趣達到您的最大值的新的計算,這可能是你所需要的。

double interest = .05000; 
double inc = 0.00125; 
double monthly = 0; 
double total = 0; 

while (interest < .08){ 

    System.out.print(interest + "\t"); 

    total = (amount * period) + (interest * amount); 
    System.out.print("\t" + total + "\t\t"); 

    monthly = ((total/period)/12); 
    System.out.println(monthly); 

    interest = interest + inc; 

} 
+1

至少解釋你的答案,代表傾銷代碼並不能幫助任何人從錯誤中學習 – redFIVE