Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 坚持做while循环_Java_Loops_While Loop_Do While - Fatal编程技术网

Java 坚持做while循环

Java 坚持做while循环,java,loops,while-loop,do-while,Java,Loops,While Loop,Do While,我正在写一个Java程序。它应该打印出年末账户余额、本年度投资、年度回报和年度数。例: 第一年-投资10000美元,投资额为10%,最终投资额为11000美元 第二年——在现有11000美元的基础上再投资10000美元,现在你有21000美元,年回报为2100美元,最终得到23100美元 一直持续到6年 我的代码打印了所有6年,但与第1年的值相同。循环有什么问题吗?非常感谢。这是我的代码: import java.io.*; import java.util.*; import java.tex

我正在写一个Java程序。它应该打印出年末账户余额、本年度投资、年度回报和年度数。例:

第一年-投资10000美元,投资额为10%,最终投资额为11000美元 第二年——在现有11000美元的基础上再投资10000美元,现在你有21000美元,年回报为2100美元,最终得到23100美元 一直持续到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);
}
import java.io.*;
导入java.util.*;
导入java.text.*;
公共课考试{
公共静态void main(字符串[]args){
最终int最大投资=5000000;
双门山;
投资额加倍;
双年率;
双重利益;
双倍合计=0;
整年=0;
Scanner myScanner=新扫描仪(System.in);
System.out.println(“输入您的投资目标金额:”);
goalAmount=myScanner.nextDouble();
如果(目标数量>最大投资){
System.out.println(“您的目标超出了允许的5000000限额”);
}
System.out.println(“输入年度投资金额:”);
amountinvestd=myScanner.nextDouble();
System.out.println(“输入预期年回报率(ex 6.50):”;
annualRate=myScanner.nextDouble();
做{
利息=投资金额*(年利率/100);
总额=投资金额+利息;
年份++;
系统输出打印项次(“年末账户余额”+总计+
“本年投资”+投资金额+“年回报率”+利息
+“年数”+年);
}而(总投资<最大投资&年<6);
System.out.println(“”);
System.out.println(“您的投资目标”+goalAmount);
System.out.println(“您的年度投资金额”+投资金额);
System.out.println(“达到目标的年数”+年);
}
}

以下是输出:

年末账户余额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

您没有保持一个连续的总数

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

Try:
total+=(投资金额+利息)


括号是不需要的,但我通常认为它们是一种很好的逻辑分组工具。

看来你也只是在计算每年储蓄的利息

更改:

do {
    interest = amountInvested * (annualRate / 100);
    total = amountInvested + interest;
    year++;
    ...
} while (total < MAX_INVESTMENT && year < 6);
do{
利息=投资金额*(年利率/100);
总额=投资金额+利息;
年份++;
...
}而(总投资<最大投资&年<6);
致:

do{
总额+=投资总额;
利息=总*年利率/100;
总额+=利息;
年份++;
...
}而(总投资<最大投资&年<6);

检查下面的循环,您没有按照说明添加10000

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循环中的最后一行,投资金额的年利率始终相同。您不能在循环中递增或递减这些值。因此,它们将始终保持不变。此外,计算利息的公式不正确,您没有在任何地方使用年份变量。应该是,

 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);
do{
利息=投资金额*(年利率/100)*年;
总额=投资金额+利息;
年份++;
系统输出打印项次(“年末账户余额”+总计+
“本年投资”+投资金额+“年回报率”+利息
+“年数”+年);
}而(总投资<最大投资&年<6);

您的计算应该取决于年数,不是吗?如果您不这样做,为什么您希望根据年数得到不同的输出?
total
并没有在每个循环中更新。在您的代码中,它只依赖于两个在循环中未更改的参数。所以它会给你同样的结果。试试
total+=
。我今年投资了22000.0年末账户余额10000.0年回报1000.0年数2。应为年末账户余额23100.0,本年投资10000.0年回报2100.0年数2。有什么想法吗?谢谢
 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);