2014-09-26 171 views
-1

我是一個初學Java的學生,試圖讓這個代碼列出8年的存款和利息。我正在嘗試爲它使用for循環。它計算利息,但只列出一年。希望有任何幫助解決這個問題。我的循環出了什麼問題

import javax.swing.JOptionPane; 
import java.text.*; 

public class RetirementGoal3 

{ 
private static DecimalFormat percentFormat = new DecimalFormat("###.##%"); 
private static DecimalFormat moneyFormat = new DecimalFormat("$###,###,###.00"); 
private static double interest; 
private static double saveAmount; 
private static double total; 
private static int Max_Year = 8; 
private static int year; 
private static double totalSave; 
private static double totalInterest; 

public static void main(String[] args) 
{ 
    calculateR(); 
} 


public static double calculateR()       
{         
String result = JOptionPane.showInputDialog(null, "Enter how much money you can save annually."); 
if(result != null) 
saveAmount = Double.parseDouble(result); 

String result1 = JOptionPane.showInputDialog(null, "Enter your interest rate(As a decimal)"); 
if(result1 != null) 
interest = Double.parseDouble(result1); 


totalSave = saveAmount; 

    for(year = 1; year <= Max_Year; ++ year) 
    { 
    totalInterest = saveAmount + saveAmount * interest; 
     JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n  With " + percentFormat.format(interest) + " Interest" + "    Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + "    " + moneyFormat.format(totalInterest)); 

    return total; 
    } 



    return total; 
    } 

} 

回答

2

您在for循環內有一個return聲明。因此,第一次迭代返回,立即退出for循環。

您有一個return語句已經在for循環之後的正確位置,因此您所要做的就是刪除for循環內的return語句。

0

更改您環路:

for(year = 1; year <= Max_Year; ++ year) 
{ 
    totalInterest = saveAmount + saveAmount * interest; 
    JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n  With " + percentFormat.format(interest) + " Interest" + "    Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + "    " + moneyFormat.format(totalInterest)); 
} 

取出回報。一旦你做了返回,它會停止執行該函數並返回給調用者。

0
for(year = 1; year <= Max_Year; ++ year) 
{ 
    totalInterest = saveAmount + saveAmount * interest; 
    JOptionPane.showMessageDialog(null, " If you save " + moneyFormat.format(totalSave) + " each year for " + Max_Year + " years" + "\n  With " + percentFormat.format(interest) + " Interest" + "    Without Interest" + "\n\nAfter year " + year + " " + moneyFormat.format(totalSave) + "    " + moneyFormat.format(totalInterest)); 

    return total; //<-- PROBLEM 

} 

問題是return循環內部。它執行一次迭代,當它返回到返回語句時,它退出循環。我想你想把它移到循環之外。您的評論後

更新:試試這個

String resultString = ""; 

for(year = 1; year <= Max_Year; year++) 
{ 
totalInterest = saveAmount + saveAmount * interest; 

    resultString += " If you save " + moneyFormat.format(totalSave) + 
        " each year for " + Max_Year + " years" + "\n" + 
        "  With " + percentFormat.format(interest) + " Interest" + 
        "    Without Interest" + "\n\nAfter year " + year + " " + 
        moneyFormat.format(totalSave) + 
        "    " + moneyFormat.format(totalInterest) + "\n"; 

} 
JOptionPane.showMessageDialog(null, resultString); 

return total; 
} 

注意,這將顯示事情你想要的方式,但我認爲你的計算出問題的地方。

+0

我想感謝你們所有人的那個循環問題,但它並沒有在單個窗格中列出所有8年。 – user3855016 2014-09-26 17:21:48

+0

它顯示多少年?嘗試將此年份的「年份= 1;年份=年份=年份+年份」更改爲此年份(年份= 1;年份=最大年份;年份++)' – qbit 2014-09-26 17:23:53

+0

它爲每年顯示一個窗格而不是列出全部8個在一個窗格中。示例年1窗格命中輸入新窗格是第2年等,我想我需要將當前for循環嵌套在另一個for循環。 – user3855016 2014-09-26 17:27:02