我的任务是创建一个程序,在运行时输出投入的年数(由用户输入),用户投入的金额(也由用户输入),投资的利息(用户输入),以及年底的总计。第二年,假设用户再次投入相同的金额,这增加了总数。例如: 如果用户以2%的利息投资200美元,为期4年,则打印出一张显示其经济收益的表格。 因此,200 + 4%的200 =总计 第二年,用户再投入200美元。 那么,下一年的总数将是总数(从上一年)*新总数的4%。 这种模式最多持续十五年。
我遇到了" investmentAmount"特别。它打印不正确。 这是我的整个代码: 的 EDITED
double investmentAmount; //the amount the user chooses to invest, expressed as a double
investmentAmount = Double.parseDouble(InputInvestmentAmount);
double Interest; // amount of interest in percent, expressed as a double
Interest = Double.parseDouble(InputInterest);
double YearsInvested; //the total years (under 15) the user wants to invest, expressed as a double
YearsInvested = Double.parseDouble(InputYearsInvested);
double Zero = 0; //a starting point for the year
double Total;
System.out.println("Years Invested \t\tAmount in Account\t\tInterest\t\tTotal");
if (YearsInvested >=16)
{
JOptionPane.showMessageDialog(null,"I am sorry. Please make sure that you are investing your money for "
+ "less than 16 years.", "Compound Investing Calculator",JOptionPane.ERROR_MESSAGE);
}
do {
Zero++; //the year
Interest = (investmentAmount*(Interest/100));
Total = investmentAmount+Interest;
System.out.format("%5.2f%32f%28f%25f%n",Zero,investmentAmount,Interest,Total);
}
while (YearsInvested <= 15 && Zero<=YearsInvested-1);
目前的输出是:
Years Invested Amount in Account Interest Total
1.00 408.000000 8.000000 408.000000
2.00 848.640000 32.640000 848.640000
3.00 1974.276096 276.996096 1974.276096
4.00 9417.219902 5468.667710 9417.219902
5.00 533830.903792 514996.463987 533830.903792
6.00 2750277940.006539 2749210278.198955 2750277940.006539
现在,我只是处理一个小问题。我需要账户中的&#34;金额&#34;不等于总数。有什么建议?我确信这是一个小问题...
答案 0 :(得分:0)
在主循环中。
Interest = (investmentAmount*=(Interest/100));
,这应该分为两行,例如:
investmentAmount = (Interest/100);
Interest = investmentAmount*(Interest/100));
答案 1 :(得分:0)
你一直在覆盖你的旧变量。一旦你覆盖了一个它不会重置的变量,请记住。
String InputInvestmentAmount = JOptionPane.showInputDialog(null, "Hello, and welcome to Compound Investing.\nThis programme will print out a titled table, that will show you \nthe amount of yearly investment, for a period up to 15 years."
+ "\nPlease enter a Yearly Investment Amount ($) below.","Compound Investing Calculator",JOptionPane.INFORMATION_MESSAGE);
String InputInterest = JOptionPane.showInputDialog(null, "Please input the interest rate in percent (%) below.","Compound Investing Calculator",JOptionPane.INFORMATION_MESSAGE);
String InputYearsInvested = JOptionPane.showInputDialog(null, "Lastly, please enter the number of years for the investment. "
+ "No numbers higher than 15, please.","Compound Investing Calculator", JOptionPane.INFORMATION_MESSAGE);
double investmentAmount; //the amount the user chooses to invest, expressed as a double
investmentAmount = Double.parseDouble(InputInvestmentAmount);
double Interest; // amount of interest in percent, expressed as a double
Interest = Double.parseDouble(InputInterest);
double YearsInvested; //the total years (under 15) the user wants to invest, expressed as a double
YearsInvested = Double.parseDouble(InputYearsInvested);
double Zero = 0; //a starting point for the year
double Total;
System.out.println("Years Invested \t\tAmount in Account\t\tInterest\t\tTotal");
if (YearsInvested >=16)
{
JOptionPane.showMessageDialog(null,"I am sorry. Please make sure that you are investing your money for "
+ "less than 16 years.", "Compound Investing Calculator",JOptionPane.ERROR_MESSAGE);
}
double IntrestMoney;
do {
Zero++; //the year
IntrestMoney = investmentAmount*(Interest/100);
Total = investmentAmount+IntrestMoney;
System.out.format("%5.2f%32f%28f%25f%n",Zero,investmentAmount,Interest,Total);
investmentAmount=Total;
}
while (YearsInvested <= 15 && Zero<=YearsInvested-1);
}
运行代码后,您将获得此输出:
Years Invested Amount in Account Interest Total
1.00 200.000000 10.000000 220.000000
2.00 220.000000 10.000000 242.000000
3.00 242.000000 10.000000 266.200000
4.00 266.200000 10.000000 292.820000
5.00 292.820000 10.000000 322.102000