【发布时间】:2017-11-26 04:16:14
【问题描述】:
我正在做一个投资计算器。
所以我会做一道数学题,我想在每个数组中循环它,其中会有 12 个。
假设我要投资 1000 美元,回报率为 4.5%。
所以我需要做 1000 * 0.045 + 1000 = 1,045 等于一个月。然后我需要做 1,045 * 0.045 + 1,045 = 1,092 这将等于第二个月,我将如何让它通过这样的循环?
我会使用 for 循环吗?还是?
这就是我目前所拥有的,也许你读了它会变得更好。但是我仍然需要创建一个循环,就像我上面给出的示例一样。
public class SimpleInvestment {
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double [] num = new double [11];
printWelcome();
double investTotal = getInvestAmount();
double rateTotal = getRate();
}
public static void printWelcome()
{
System.out.println("Welcome to the Investment Calulator");
}
public static double getInvestAmount()
{
double amountInvest;
System.out.print("Hou much will you be investing? ");
amountInvest = input.nextDouble();
while (amountInvest <= 0)
{
System.out.println("Amount must be greater than 0. Try again.");
System.out.print("How much will you be investing? ");
amountInvest = input.nextDouble();
}
return amountInvest;
}
public static double getRate()
{
double rate;
System.out.print("What will be the rate of return?");
rate = input.nextDouble();
while (rate < 0 )
{
System.out.println("Rate must be greater than 0. Try again.");
System.out.print("What will be the rate of return?");
rate = input.nextDouble();
}
return rate;
}
public static void calculateInterst( double num[], double investTotal, double rateTotal)
{
double total;
rateTotal = rateTotal / 100;
total = investTotal * rateTotal + investTotal;
}
}
【问题讨论】:
-
为什么不做 1000 * 1.045 一个月呢?等等。投资 x 个月或同时仍有投资 $amount 现在获得利息。
-
如果您希望对您的代码进行审查,请发布here。如果您询问如何重复计算
1000 * 0.045 + 1000,请删除所有不相关的代码,如欢迎和用户输入相关代码。见minimal reproducible example