1

我有一个项目,我必须编写一个程序,提示用户输入初始投资金额和目标投资金额,并计算从初始金额增长到固定利率目标金额需要多少年(即: 5%)。(使用 WHILE 循环)。打印出每年的结果。例如,如果您选择在 5 年内投资 1,000 美元: 第 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);

在这个循环的末尾有一个分号。去掉它。

因此,printfor 循环之后的下一组语句变为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.
} 

这些只会执行一次。


更新: -

您可以使用以下代码获取每年的总利息和最终输出

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

分号 atfor(; 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 回答