while循环中的加法运算符(Java)

时间:2014-01-30 01:09:50

标签: java loops for-loop while-loop addition

我刚刚开始学习Java,我遇到了以下问题。

我在while循环中需要有关for循环的帮助。

while (difference > epsilon) {
    for (int u = 0; u <= N, u++) {
        for (int d = 0; d <= N, d++) {
            formulaLong += Math.pow(E, -rT) * Math.pow(0.5, N) * productofVC[u][d];
        }
    }
}

每次while循环运行时,变量formulaLong都会从前一个while循环添加到上一个formulaLong。如何对此进行编码,以便formulaLong为我提供当前while循环的公式?

2 个答案:

答案 0 :(得分:0)

只需添加此行..正如我从您的问题中理解的那样!!

while(difference > epsilon){
 for (int u = 0; u <= N, u++){
  for (int d = 0; d <= N, d++){
   formulaLong += Math.pow(E, -rT)*Math.pow(0.5, N)*productofVC[u][d];
  }
 }
 formulaLong = 0; // so you reset the value after the for loop
}

答案 1 :(得分:0)

可以像这样在你的top for循环上面重新初始化:

while(difference > epsilon){
  formulaLong = 0;//reset
  for (int u = 0; u <= N, u++){
     for (int d = 0; d <= N, d++){
         formulaLong += Math.pow(E, -rT)*Math.pow(0.5, N)*productofVC[u][d];
     }
  }
}

不确定你的while循环会如何停止。

相关问题