简体   繁体   English

卡在do-while循环中

[英]Stuck with do-while loop

I am writing a Java program. 我正在编写一个Java程序。 It supposed to be print out yearend account balance, invested this year, annual return, and number of the year. 它应该打印出年末帐户余额,今年投资,年收益和年数。 EX: 例如:

Year 1 – Invest $10,000 @ 10% you end up with $11,000 Year 2 – Invest another $10,000 on top of the $11,000 you already have so now you have $21,000 and you make $2,100 in annual return so you end up with $23,100 and keep going until reach 6 years. 第一年–在10%的情况下投资10,000美元,您最终将获得$ 11,000第二年–在您已经拥有的$ 11,000之上再投资10,000美元,所以现在您有$ 21,000,并且您的年收益为$ 2,100,因此最终您将获得$ 23,100并继续努力直到达到6年

My code printed all 6 years but with same value as year 1. So anything wrong with the loop? 我的代码打印了全部6年,但与第1年的值相同。那么循环有什么问题吗? Thanks so much. 非常感谢。 Here my code: 这是我的代码:

import java.io.*;
import java.util.*;
import java.text.*;
public class ExamFive {
public static void main(String[] args) {
    final int MAX_INVESTMENT = 5000000;
    double goalAmount;
    double amountInvested;
    double annualRate;
    double interest;
    double total= 0;
    int year = 0;
    Scanner myScanner = new Scanner(System.in);
    System.out.println("Enter your investment goal amount: ");
    goalAmount = myScanner.nextDouble();
    if (goalAmount > MAX_INVESTMENT) {
        System.out.println("Your goal is outside the allowed 5,000,000 limit");
    }
    System.out.println("Enter the amount annually invested: ");
    amountInvested = myScanner.nextDouble();
    System.out.println("Enter the expected annual return rate (ex 6.50): ");
    annualRate = myScanner.nextDouble();

    do {
        interest = amountInvested * (annualRate / 100);
        total = amountInvested + interest;
        year++;
        System.out.println("Yearend account balance " + total + 
                " Invested this year " + amountInvested + " Annual  return " + interest
                + " number of years " + year);

    } while (total < MAX_INVESTMENT && year < 6);

    System.out.println(" ");
    System.out.println("Your investment goal " + goalAmount);
    System.out.println("Your annualinvestment amount " + amountInvested);
    System.out.println("Number of years to reach your goal " + year);
}

} }

Here is the output: 这是输出:

Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 1 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 2 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 3 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 4 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 5 Yearend account balance 11000.0 Invested this year 10000.0 Annual return 1000.0 number of years 6 年末账户余额11000.0今年投资了10000.0年回报1000.0年数1年末账户余额11000.0今年投资了10000.0年回报1000.0年数2年末账户余额11000.0今年投资了10000.0年回报1000.0年数3年末账户余额11000.0投资了今年10000.0年收益1000.0年数4年末账户余额11000.0年投资10000.0年收益1000.0年数5年末账户余额11000.0年本年投资10000.0年收益1000.0年数6

You aren't keeping a running total. 您并没有保持运行总数。

Looks like maybe a 3rd or 4th week tutorial but it's a fair question at least you asked what's wrong. 看起来可能是第3周或第4周的教程,但这是一个公平的问题,至少您问过哪里出了问题。

Try: total += (amountInvested + interest); 尝试: total += (amountInvested + interest);

parenthesis brackets aren't needed but I usually find them a great logical grouping device. 不需要括号,但是我通常会发现它们是一个很好的逻辑分组设备。

It seems that you're also calculating the interest on the annual savings only. 看来您也只是在计算每年节省的利息。

Change: 更改:

do {
    interest = amountInvested * (annualRate / 100);
    total = amountInvested + interest;
    year++;
    ...
} while (total < MAX_INVESTMENT && year < 6);

To: 至:

do {
    total += amountInvested;
    interest = total * annualRate / 100;
    total += interest;
    year++;
    ...
} while (total < MAX_INVESTMENT && year < 6);

Check the loop below you are not adding 10,000 as you stated 检查下面的循环,您没有像所说的那样增加10,000

do {
    interest = amountInvested * (annualRate / 100);
    total = amountInvested + interest;
    year++;
    System.out.println("Yearend account balance " + total + 
            " Invested this year " + amountInvested + " Annual  return " + interest
            + " number of years " + year);

}

Add following line 添加以下行

amountInvested += (total + 10000);

as the last line in do while loop 作为do while循环的最后一行

amountInvested, annualRate are always the same. amountInvested,annualRate始终相同。 you are not incrementing or decrementing these values in the loop. 您不会在循环中递增或递减这些值。 So, they will always remain the same. 因此,它们将始终保持不变。 Also, the formula to caculate the interest is incorrect, you are not using year variable anywhere. 另外,计算利息的公式不正确,您没有在任何地方使用年份变量。 it should be, 它应该是,

 do {
    interest = amountInvested * (annualRate / 100) * year;
    total = amountInvested + interest;
    year++;
    System.out.println("Yearend account balance " + total + 
            " Invested this year " + amountInvested + " Annual  return " + interest
            + " number of years " + year);

} while (total < MAX_INVESTMENT && year < 6);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM