1

ユーザーに初期投資額と目標投資額を求めるプログラムを作成し、固定金利で初期額から目標額まで成長するのに何年かかるかを計算する必要があるプロジェクトがあります(つまり、 :5%)。(WHILE ループを使用します)。毎年の結果を印刷します。たとえば、$1,000 を 5 年間投資することを選択した場合: 1 年目 1005 年 2 年目 1011 など:

Java プログラムをコンパイルすることはできましたが、初期投資と目標投資を 5% の利息でユーザーに促すことしかできませんでした。出力が正しくありませんでした。私は何を間違っていますか?これが私のコードです。

import static java.lang.System.out;
import java.util.Scanner;
import java.io.*;

public class Investment_Calculator {//main
public static void main(String[] args) {//begins body

double principal = 1;//initial amount investing
double interest = 1;
double rate = 0.05;//the fixed interest amount
int years = 1;//amout of years it will take to achieve goal
double goal = 1;
double total = 1;

Scanner myScanner = new Scanner(System.in);

System.out.println("*************************************** ");
System.out.println("* Welcome to the Investment Calculator * ");
System.out.println("*************************************** ");

System.out.println ("Enter your initial investment amount: if you want to exit enter   0.");
int inputNumber = myScanner.nextInt();
principal = inputNumber; 

if (inputNumber == 0){//if num = 0 exit class

System.exit(0);
}

System.out.println ("Enter your goal investment amount: ");
int inputNumber2 = myScanner.nextInt ();
goal = inputNumber2;

System.out.println ("The fixed interest rate is 5%");

total= principal; // start with our initial amount of money
for (; total < goal; total= (1+interest)*total);   

{   System.out.print("The number of years you must invest to meet your goal of $ ");
 System.out.print(goal);
 System.out.print(" is");   
 System.out.println(years);
 System.out.println(" years");

} 
}

}
4

3 に答える 3

6
for (; total < goal; total= (1+interest)*total);

このループの最後にセミコロンがあります。それを除く。

printそのため、 for ループの後のステートメントの次のセットはunnamed block: -

{   
 System.out.print("The number of years you must invest to meet your goal of $ ");
 System.out.print(goal);
 System.out.print(" is");   
 System.out.println(years);
 System.out.println(" years");

    years++;  <-- // Increment `years` here.
} 

そして、それらは一度だけ実行されます。


更新: -

以下のコードを使用して、各年の totalInterest と最終出力を取得できます。

int years = 1;
while (principal <= goal) {
    double totalInterest = principal * rate * years / 100.0;
    principal = principal + totalInterest;

    System.out.println("Interest amount for year: " + years + 
                       " = " + totalInterest);

    years++;
}

System.out.print("The number of years you must invest to meet your goal of $ ");
System.out.print(goal);
System.out.print(" is");   
System.out.println(years);
System.out.println(" years");
于 2012-10-27T15:35:57.937 に答える
1

at のセミコロンfor(; total < goal; total = (1+interest) * total);が問題を引き起こしています。

に変更します:

for (; total < goal; total= (1+interest)*total)   
{   
 System.out.print("The number of years you must invest to meet your goal of $ ");
 System.out.print(goal);
 System.out.print(" is");   
 System.out.print(years);
 System.out.println(" years");
} 
于 2012-10-27T15:38:12.557 に答える
0

これを現在の for- ステートメントに置き換えます

int year = 0;
    for (; total < goal; total= ((1+rate)*total), ++year)
    {
     ...
     // whatever you were doing in for loop
    }
System.out.println (year);
于 2012-10-27T15:58:43.243 に答える