坚持使用do-while循环

时间:2014-04-09 04:42:03

标签: java loops while-loop do-while

我正在编写一个Java程序。它应该打印出年终帐户余额,今年投资,年度回报和年度数量。 EX:

第1年 - 投资10,000美元@ 10%,最终获得11,000美元 2年级 - 在你已经拥有的11,000美元的基础上再投资10,000美元,所以现在你有21,000美元,你的年回报率是2,100美元,所以最终得到23,100美元 并继续坚持到6年。

我的代码打印了所有6年,但与第1年的价值相同。所以循环有什么问题?非常感谢。这是我的代码:

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

}

这是输出:

Yearend账户余额11000.0今年投资10000。0年回报1000.0年数1 Yearend账户余额11000.0今年投资10000。0年回报1000.0年数2 Yearend账户余额11000.0今年投资10000。0年回报1000.0年数3 Yearend账户余额11000.0今年投资10000。0年回报1000.0年数4 Yearend账户余额11000.0今年投资10000。0年回报1000.0年数5 Yearend账户余额11000.0今年投资10000。0年回报1000.0年数6

4 个答案:

答案 0 :(得分:0)

你没有保持总计。

看起来可能是第3或第4周的教程,但这是一个公平的问题,至少你问过什么是错的。

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

不需要括号括号,但我通常会发现它们是一个很棒的逻辑分组设备。

答案 1 :(得分:0)

您似乎也在计算每年节省的利息。

变化:

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

要:

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

答案 2 :(得分:0)

检查下面的循环,您没有按照说明添加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);

}

添加以下行

amountInvested += (total + 10000);

作为do while循环的最后一行

答案 3 :(得分:0)

amountInvested,annualRate总是一样的。您没有在循环中递增或递减这些值。所以,它们将始终保持不变。此外,计算兴趣的公式是不正确的,你不是在任何地方使用年变量。它应该是,

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