我在编写这个程序时遇到了一些问题。我正在制作一个程序,将用户输入投资,年份和兴趣,并将其输出为表格。这一切似乎都正常工作 - 除了输出没有正确添加,并没有计算兴趣。这是我的代码:
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 adouble
YearsInvested = Double.parseDouble(InputYearsInvested);
double Year = 0; //a starting point for the year
double Total;
do {
Year++; //the year
Interest = (investmentamount * (Interest / 100));
Total = investmentamount + Interest;
System.out.format("%5.0f%32.2f%28.2f%25.2f%n", Year, investmentamount, Interest, Total);
investmentAmount = Total + investmentamount;
} while (YearsInvested <= 15 && Year <= YearsInvested - 1);
例如,如果我输入50美元,1%的利息,15年,我得到无限作为答案。
Years Invested Amount in Account Interest Total
1 50.00 0.50 50.50
2 100.50 0.50 101.00
3 201.50 1.01 202.52
4 404.02 4.09 408.11
5 812.13 33.22 845.35
6 1657.48 550.66 2208.14
7 3865.61 21286.57 25152.19
8 29017.80 6176894.63 6205912.43
9 6234930.23 385125070004.00 385131304934.23
10 385137539864.45 1483261220014673600000.001483261220399811100000.00
11 1483261220784948600000.0022000638479419373000000000000000000000000.0022000638479419373000000000000000000000000.00
1222000638479419373000000000000000000000000.004840280935021083600000000000000000000000000000000000000000000000000000000000000.004840280935021083600000000000000000000000000000000000000000000000000000000000000.00
134840280935021083600000000000000000000000000000000000000000000000000000000000000.00234283195299285770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00234283195299285770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00
14234283195299285770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00 Infinity Infinity
我为输出的混乱道歉。
答案 0 :(得分:3)
问题是您正在重用变量并覆盖它。
double investmentAmount; //the amount the user chooses to invest, expressed as a double
investmentAmount = Double.parseDouble(InputInvestmentAmount);
Double OrginalInvestment;
OrginalInvestment=investmentAmount;//this is too keep your orginal investment
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,IntrestMoney,Total);
investmentAmount=Total+OrginalInvestment; //
}
while (YearsInvested <= 15 && Zero<=YearsInvested-1);
}
如果你使用200美元作为投资,10%作为你的费率,5年作为你的期限,这是输出:
Years Invested Amount in Account Interest Total
1.00 200.000000 20.000000 220.000000
2.00 420.000000 42.000000 462.000000
3.00 662.000000 66.200000 728.200000
4.00 928.200000 92.820000 1021.020000
5.00 1221.020000 122.102000 1343.122000
修改强>
因为太长的小数点会弄乱你的桌子我建议将你的答案四舍五入到你可以使用以下方法:
public static double round(double value, int places) {
if (places < 0) {
throw new IllegalArgumentException();
}
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
请注意,您必须调用round()
方法更改Total
值:
Total = round((investmentAmount+IntrestMoney),4);