java 用java计算利率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32875448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating interest rate in java
提问by robinweew
I'm making a method that are supposed to calculate the interest rate of an amount over a certain period of years (these values are already defined in the parameters). This is the code i have so far:
我正在制定一种方法,该方法应该计算一定年数内某个金额的利率(这些值已在参数中定义)。这是我到目前为止的代码:
public void balance(int amount, double rate, int year){
double yearlyRate = amount * rate;
double totalAmount;
System.out.print(amount + " :- " + " grows with the interest rate of " + rate);
for (int i = 0; i <= year; i++ ){
for ( int j = amount; j)
totalAmount = amount + yearlyRate;
System.out.println(i + " " + totalAmount);
}
}
I was on my way to make the nested for-loop as you can see there where there is missing code. Here i ran in too some trouble. The first for-loop runs through the years and the other are supposed to calculate the total amount. To be clear the years that are defined in the variable "int year" let's say that it is 7, then the program are supposed to calculate the growth of the amount each year so:
我正在制作嵌套的 for 循环,因为您可以看到那里缺少代码。在这里,我遇到了一些麻烦。第一个 for 循环贯穿多年,另一个应该计算总量。为了清楚在变量“int year”中定义的年份,假设它是 7,那么程序应该计算每年的增长量,所以:
year1 totalAmount
year2 totalAmount
year3 totalAmount
and so on.....
The main method looks like this:
主要方法如下所示:
public void exerciceG(Prog1 prog1) {
System.out.println("TEST OF: balance");
prog1.balance(1000, 0.04, 7);
}
I appreciate any help i can get!
我很感激我能得到的任何帮助!
回答by DSlomer64
Here's one change to make, but there may be a lot else to do as I mentioned in comment:
这是要进行的一项更改,但正如我在评论中提到的那样,可能还有很多事情要做:
totalAmount = totalAmount + amount + yearlyRate;
which could be written:
可以写成:
totalAmount += amount + yearlyRate;
You also might want to drop the for j
loop since it doesn't do anything as is.
您可能还想删除for j
循环,因为它不会按原样执行任何操作。
EDIT
编辑
This is a guess since I'm not sure we know what the objective is, but how about:
这是一个猜测,因为我不确定我们知道目标是什么,但是如何:
public static void balance(int amount, double rate, int year){
double yearlyInterestPaid ;
double totalAmount = amount;
System.out.println(amount + " :- " + " grows with the interest rate of " + rate);
for (int i = 0; i <= year; i++ ){
yearlyInterestPaid = totalAmount * rate;
totalAmount += yearlyInterestPaid;
System.out.println(i + " " + totalAmount);
}
}
This is the output:
这是输出:
TEST OF: balance
1000 :- grows with the interest rate of 0.04
0 1040.0
1 1081.6
2 1124.8639999999998
3 1169.85856
4 1216.6529024
5 1265.319018496
6 1315.93177923584
7 1368.5690504052736
It's reasonable to assume that this is the objective.
假设这是目标是合理的。
回答by Lucifer
I think the answer you are looking for is this
我想你正在寻找的答案是这个
for (int i = 0; i <= year; i++ ){
amount = amount + yearlyRate;
System.out.println(i + " " + amount);
}