简体   繁体   English

Java for循环传递变量

[英]Java for loop passing variable

I have the following code:我有以下代码:

double getTotalPayments(){
    for (int i = 0 ; i == periodsPerYear * years ; i++){
      double balance =+ Math.round(initialBalance - (monthlyPayment-((interestRate/periodsPerYear) *initialBalance))*100.00)/100.00;
      initialBalance =- balance;
      if(i == periodsPerYear*years){
        return balance;
        break;
      }
    }
    return balance;
  }

I'm trying to pass the double variable 'balance' for the method to return.我正在尝试为要返回的方法传递双变量“balance”。 I have to use the for loop to calculate the total amount of payments.我必须使用 for 循环来计算付款总额。 Any suggestions on how to fix it?关于如何解决它的任何建议? I've tired everything I can think of.我已经厌倦了我能想到的一切。

I think you're just missing a few things within the code.我认为您只是在代码中遗漏了一些东西。 It would probably go something like this.它可能是 go 这样的东西。 You were just missing the bits within the method declaring that it's either a public or private method, and that you weren't calling in the Int Variable balance.您只是缺少声明它是公共方法或私有方法的方法中的位,并且您没有调用 Int 变量余额。

public double getTotalPayments(int balance){
for (int i = 0 ; i == periodsPerYear * years ; i++){
  double balance =+ Math.round(initialBalance - (monthlyPayment-((interestRate/periodsPerYear) *initialBalance))*100.00)/100.00;
  initialBalance =- balance;
  if(i == periodsPerYear*years){
    return balance;
    break;
  }
}
return balance;

Please let me know if that works!如果可行,请告诉我!

It should be它应该是

double balance += Math.round(initialBalance - 
(monthlyPayment-((interestRate/periodsPerYear) 
 *initialBalance))*100.00)/100.00;

initialBalance -= balance;

How can you return balance when "balance" is only a local variable in for loop?当“balance”只是 for 循环中的局部变量时,如何返回 balance? And another like @NomadMaker said that the condition for the for loop should be "i < periodPerYear * years".另一个类似@NomadMaker 的人说 for 循环的条件应该是“i < periodPerYear * years”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM