-4

宿題にこの問題があり、プログラムを実行して値を入力すると、たとえば、この「30 14730.576123040439」を取得する年数に 30 を入力すると、1 つの結果しか得られないという問題が 30 回発生します。適切な結果を表示するために私ができることのアイデア(つまり、30年目の正しい結果を30回ではなく、1年から30年)。

   public class FinancialApp57 
   {
       public static void main(String[] args) 
       {
           //variables
           double intRate, invAmt;
           int yrs;
           String inv = JOptionPane.showInputDialog("Enter an investment amount:");
           invAmt = Double.parseDouble(inv);
           String rate = JOptionPane.showInputDialog("Enter an intrest rate: ");
           intRate = Double.parseDouble(rate);
           String yr = JOptionPane.showInputDialog
                   ("Enter the lenght of investment(in years): ");
           yrs = Integer.parseInt(yr);

           futureInvestmentValue(invAmt,intRate/12,yrs);
       }

       public static double futureInvestmentValue
               (double investmentAmount,double monthlyIntrestRate,int years)
       {
           double FutureValue = 0;
           int i = 1;
           FutureValue=
                   investmentAmount*Math.pow((1+monthlyIntrestRate/100),years*12);

           while(i<years)
           {            
               System.out.println(years+"\t" + FutureValue);
               i++;
           }
           return FutureValue;

       }
   }
4

2 に答える 2

2

FutureValue を 30 回再計算するのではなく、1 回だけ計算してから 30 回出力します。FutureValue の計算をループに入れ、それに応じてそのパラメーターを調整します

于 2013-02-03T02:18:42.657 に答える
1

コードが出力する値は「years」です。「30」と入力すると、int years = 30 になります。(1,2,3,4,5....) のようなものが必要な場合は、「i」を出力する必要があります。 「年」の代わりに、次のようにする必要があります。

while(i<=years) {            
    System.out.println(i+"\t" + FutureValue);
    i++;
}
return FutureValue;
于 2013-02-03T02:42:06.123 に答える