Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的投资代码_Java - Fatal编程技术网

Java中的投资代码

Java中的投资代码,java,Java,我有一个项目,我必须编写一个程序,提示用户输入初始投资金额和目标投资金额,并计算在固定利率(即:5%)下从初始金额增长到目标金额需要多少年。使用WHILE循环。打印出每年的结果。例如,如果您选择在5年内投资1000美元: 第1005年 第2年1011 等等: 我能够编译java程序,但我只能提示用户进行初始投资和目标投资,并支付5%的利息。输出不正确。我在做什么?这是我的密码 而这些只会执行一次 更新:- 您可以使用下面的代码获取每年的总利息和最终输出 int years = 1; while

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

我能够编译java程序,但我只能提示用户进行初始投资和目标投资,并支付5%的利息。输出不正确。我在做什么?这是我的密码

而这些只会执行一次

更新:-

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

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");
而这些只会执行一次

更新:-

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

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");
表示的分号;总目标;总计=1+利息*总计;正在引发问题

将其更改为:

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");
} 
表示的分号;总目标;总计=1+利息*总计;正在引发问题

将其更改为:

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");
} 

将其替换为当前的for语句

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

将其替换为当前的for语句

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

只是一个提示:如果你正确地缩进代码,那么理解发生的事情就会容易得多。只是一个提示:如果你正确地缩进代码,那么理解发生的事情就会容易得多。好的,我去掉了分号。数学仍然不完整。输出是这样的。**********************************************************欢迎使用投资计算器*********************************************输入您的初始投资金额:如果要退出,请输入0。1000输入您的目标投资金额:5000固定利率为5%您必须投资的年数以实现5000美元的目标。0是1年您必须投资的年数以实现5000美元的目标。0是1年您必须投资的年数以实现5000美元的目标。0是1年过程已完成。@user1743771。这是因为你没有改变你的目标和在循环中的年数。@user1743771。你的预期产出是多少?如果我以5%的利率投资1000英镑,我的预期产出是多少年才能获得5000英镑。@user1743771。其余的变量是你在原始代码中已经声明的变量,本金,利率,年份,goalOk I去掉了分号。数学仍然不完整。输出是这样的。**********************************************************欢迎使用投资计算器*********************************************输入您的初始投资金额:如果要退出,请输入0。1000输入您的目标投资金额:5000固定利率为5%您必须投资的年数以实现5000美元的目标。0是1年您必须投资的年数以实现5000美元的目标。0是1年您必须投资的年数以实现5000美元的目标。0是1年过程已完成。@user1743771。这是因为你没有改变你的目标和在循环中的年数。@user1743771。你的预期产出是多少?如果我以5%的利率投资1000英镑,我的预期产出是多少年才能获得5000英镑。@user1743771。其余的变量是您在原始代码中已经声明的变量、本金、利率、年份和目标