正在计算 java 中的利率
Calculating interest rate in java
我正在制作一种方法,该方法应该计算一定年限内金额的利率(这些值已在参数中定义)。
这是我到目前为止的代码:
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);
}
}
我正在制作嵌套的 for 循环,您可以看到那里缺少代码。我运行这里太麻烦了。第一个 for 循环贯穿年份,另一个循环计算总量。要清楚变量中定义的年份 "int year" 假设它是 7,那么程序应该计算每年的增长量所以:
year1 totalAmount
year2 totalAmount
year3 totalAmount
and so on.....
主要方法如下所示:
public void exerciceG(Prog1 prog1) {
System.out.println("TEST OF: balance");
prog1.balance(1000, 0.04, 7);
}
我很感激能得到的任何帮助!
我想你正在寻找的答案就是这个
for (int i = 0; i <= year; i++ ){
amount = amount + yearlyRate;
System.out.println(i + " " + amount);
}
这里有一项更改,但正如我在评论中提到的那样,可能还有很多其他事情要做:
totalAmount = totalAmount + amount + yearlyRate;
可以写成:
totalAmount += amount + yearlyRate;
您可能还想删除 for j
循环,因为它不会按原样执行任何操作。
编辑
这是一个猜测,因为我不确定我们是否知道 objective 是什么,但是如何:
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);
}
}
这是输出:
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
可以合理地假设这是 objective。
我正在制作一种方法,该方法应该计算一定年限内金额的利率(这些值已在参数中定义)。 这是我到目前为止的代码:
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);
}
}
我正在制作嵌套的 for 循环,您可以看到那里缺少代码。我运行这里太麻烦了。第一个 for 循环贯穿年份,另一个循环计算总量。要清楚变量中定义的年份 "int year" 假设它是 7,那么程序应该计算每年的增长量所以:
year1 totalAmount
year2 totalAmount
year3 totalAmount
and so on.....
主要方法如下所示:
public void exerciceG(Prog1 prog1) {
System.out.println("TEST OF: balance");
prog1.balance(1000, 0.04, 7);
}
我很感激能得到的任何帮助!
我想你正在寻找的答案就是这个
for (int i = 0; i <= year; i++ ){
amount = amount + yearlyRate;
System.out.println(i + " " + amount);
}
这里有一项更改,但正如我在评论中提到的那样,可能还有很多其他事情要做:
totalAmount = totalAmount + amount + yearlyRate;
可以写成:
totalAmount += amount + yearlyRate;
您可能还想删除 for j
循环,因为它不会按原样执行任何操作。
编辑
这是一个猜测,因为我不确定我们是否知道 objective 是什么,但是如何:
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);
}
}
这是输出:
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
可以合理地假设这是 objective。