Java 如何简化下面的类

Java 如何简化下面的类,java,simplify,Java,Simplify,嗨,我是编程新手,我想知道如何简化以下代码。是否有一种方法可以设置一个变量,该变量将改变,而不是设置1-6个月 double RATE; double SAVING; double MONTH1; double MONTH2; double MONTH3; double MONTH4; double MONTH5; double MONTH6; System.out.print("Enter the monthly saving amount: "); SAVING = input.next

嗨,我是编程新手,我想知道如何简化以下代码。是否有一种方法可以设置一个变量,该变量将改变,而不是设置1-6个月

double RATE;
double SAVING; 
double MONTH1;
double MONTH2;
double MONTH3;
double MONTH4;
double MONTH5;
double MONTH6;

System.out.print("Enter the monthly saving amount: ");
SAVING = input.nextDouble();

RATE = 1.00417;

MONTH1 = SAVING * RATE;
MONTH2 = (MONTH1 + SAVING) * RATE;
MONTH3 = (MONTH2 + SAVING) * RATE;
MONTH4 = (MONTH3 + SAVING) * RATE;
MONTH5 = (MONTH4 + SAVING) * RATE;
MONTH6 = (MONTH5 + SAVING) * RATE;

System.out.println("After the sixth month, the account value is $" + (float) MONTH6);

你在这里做什么:

MONTH1 = SAVING * RATE;
MONTH2 = (MONTH1 + SAVING) * RATE;
MONTH3 = (MONTH2 + SAVING) * RATE;
MONTH4 = (MONTH3 + SAVING) * RATE;
MONTH5 = (MONTH4 + SAVING) * RATE;
MONTH6 = (MONTH5 + SAVING) * RATE;
你已经一遍又一遍地写了同样的东西

对于这样的重复性任务,循环总是一个更好的解决方案

使用循环,您甚至不需要所有的
MONTH
变量。下面是一个使用循环消除重复代码在
totalMonths
months之后计算帐户值的示例:

int accountValue = 0;
int monthNumber = 0;

while (monthNumber < totalMonths) {                // Do what's inside the brackets as long as monthNumber is less than totalMonths.
    accountValue = (accountValue + saving) * rate; // Update the account value with saving and rate.
    monthNumber = monthNumber + 1;                 // Increase monthNumber by one.
}

System.out.println("After " + totalMonths + " months, the account value is $" + accountValue);
int accountValue=0;
int monthNumber=0;
而(monthNumber
在上述代码末尾,
accountValue
将在
totalMonths
months过去后具有总帐户值

这是一个非常简单的例子。尝试一下,确保你100%理解它


请注意,我用小写(,确切地说)写变量名,因为这在Java中很常见。

您在这里做什么:

MONTH1 = SAVING * RATE;
MONTH2 = (MONTH1 + SAVING) * RATE;
MONTH3 = (MONTH2 + SAVING) * RATE;
MONTH4 = (MONTH3 + SAVING) * RATE;
MONTH5 = (MONTH4 + SAVING) * RATE;
MONTH6 = (MONTH5 + SAVING) * RATE;
你已经一遍又一遍地写了同样的东西

对于这样的重复性任务,循环总是一个更好的解决方案

使用循环,您甚至不需要所有的
MONTH
变量。下面是一个使用循环消除重复代码在
totalMonths
months之后计算帐户值的示例:

int accountValue = 0;
int monthNumber = 0;

while (monthNumber < totalMonths) {                // Do what's inside the brackets as long as monthNumber is less than totalMonths.
    accountValue = (accountValue + saving) * rate; // Update the account value with saving and rate.
    monthNumber = monthNumber + 1;                 // Increase monthNumber by one.
}

System.out.println("After " + totalMonths + " months, the account value is $" + accountValue);
int accountValue=0;
int monthNumber=0;
而(monthNumber
在上述代码末尾,
accountValue
将在
totalMonths
months过去后具有总帐户值

这是一个非常简单的例子。尝试一下,确保你100%理解它


请注意,我用小写(,确切地说)写变量名,因为这在Java中很常见。

您在这里做什么:

MONTH1 = SAVING * RATE;
MONTH2 = (MONTH1 + SAVING) * RATE;
MONTH3 = (MONTH2 + SAVING) * RATE;
MONTH4 = (MONTH3 + SAVING) * RATE;
MONTH5 = (MONTH4 + SAVING) * RATE;
MONTH6 = (MONTH5 + SAVING) * RATE;
你已经一遍又一遍地写了同样的东西

对于这样的重复性任务,循环总是一个更好的解决方案

使用循环,您甚至不需要所有的
MONTH
变量。下面是一个使用循环消除重复代码在
totalMonths
months之后计算帐户值的示例:

int accountValue = 0;
int monthNumber = 0;

while (monthNumber < totalMonths) {                // Do what's inside the brackets as long as monthNumber is less than totalMonths.
    accountValue = (accountValue + saving) * rate; // Update the account value with saving and rate.
    monthNumber = monthNumber + 1;                 // Increase monthNumber by one.
}

System.out.println("After " + totalMonths + " months, the account value is $" + accountValue);
int accountValue=0;
int monthNumber=0;
而(monthNumber
在上述代码末尾,
accountValue
将在
totalMonths
months过去后具有总帐户值

这是一个非常简单的例子。尝试一下,确保你100%理解它


请注意,我用小写(,确切地说)写变量名,因为这在Java中很常见。

您在这里做什么:

MONTH1 = SAVING * RATE;
MONTH2 = (MONTH1 + SAVING) * RATE;
MONTH3 = (MONTH2 + SAVING) * RATE;
MONTH4 = (MONTH3 + SAVING) * RATE;
MONTH5 = (MONTH4 + SAVING) * RATE;
MONTH6 = (MONTH5 + SAVING) * RATE;
你已经一遍又一遍地写了同样的东西

对于这样的重复性任务,循环总是一个更好的解决方案

使用循环,您甚至不需要所有的
MONTH
变量。下面是一个使用循环消除重复代码在
totalMonths
months之后计算帐户值的示例:

int accountValue = 0;
int monthNumber = 0;

while (monthNumber < totalMonths) {                // Do what's inside the brackets as long as monthNumber is less than totalMonths.
    accountValue = (accountValue + saving) * rate; // Update the account value with saving and rate.
    monthNumber = monthNumber + 1;                 // Increase monthNumber by one.
}

System.out.println("After " + totalMonths + " months, the account value is $" + accountValue);
int accountValue=0;
int monthNumber=0;
而(monthNumber
在上述代码末尾,
accountValue
将在
totalMonths
months过去后具有总帐户值

这是一个非常简单的例子。尝试一下,确保你100%理解它


请注意,我用小写(,确切地说),因为这在Java中很常见。

伙计们,请不要因为1)缺乏知识/经验,或2)缺乏先前的研究而对问题进行评分。S.O.是许多具有各种技能水平的程序员都可以学习的资源,我们不要通过降低新用户的问题等级来阻止他们,除非显然没有人努力解决这个问题。@EvanBechtol我没有否决,但“否决”按钮上的工具提示解释了什么时候否决是合适的:“这个问题没有显示任何研究工作;它不清楚或没有用处因此,缺乏事先的研究是否决投票的一个很好的理由。坦率地说,这个问题是边界问题。我不是说你投了票!我只是注意到有人投了反对票,认为这有点不必要。OP提出了一个合理的问题。他/她能做更多的研究吗?绝对地但我们必须记住,S.O.经常作为顶级搜索出现,所以t