【问题标题】:Investment code in JavaJava中的投资代码
【发布时间】:2012-10-27 15:34:24
【问题描述】:

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

} 
}

}

【问题讨论】:

  • 只是一个提示:如果你正确缩进你的代码,就会更容易理解发生了什么。

标签: java


【解决方案1】:
for (; total < goal; total= (1+interest)*total);

您在此循环的末尾有一个分号。删除它。

因此,for 循环之后的下一组 print 语句变为 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");

【讨论】:

  • 好的,我删除了分号。数学仍然没有加起来。输出是这样的。 ****************************************** * 欢迎使用投资计算器 * **** *********************************** 输入您的初始投资金额:如果要退出输入0. 1000输入您的目标投资金额:5000 固定利率为 5% 您必须投资的年数才能达到 5000.0 美元的目标是 1 年 您必须投资的年数才能达到 5000.0 美元的目标是 1 年 年数您必须投资以实现您的目标 $5000.0 是 1 年过程完成。
  • @user1743771。那是因为您没有在循环内更改 goalyears
  • @user1743771。您的预期输出是什么?
  • 我的预期输出是如果我以 5% 的利率投资 1000 的初始金额,我需要多少年才能获得 5000。
  • @user1743771。其余变量是您在原始代码中已声明的变量,principalrateyearsgoal
【解决方案2】:

for(; total &lt; 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");
} 

【讨论】:

    【解决方案3】:

    用你当前的 for- 语句替换它

    int year = 0;
        for (; total < goal; total= ((1+rate)*total), ++year)
        {
         ...
         // whatever you were doing in for loop
        }
    System.out.println (year);
    

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 2016-10-02
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多