ユーザーに初期投資額と目標投資額を求めるプログラムを作成し、固定金利で初期額から目標額まで成長するのに何年かかるかを計算する必要があるプロジェクトがあります(つまり、 :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");
}
}
}