简体   繁体   English

在while循环内更​​改变量

[英]Variable changing within a while loop

I'm trying to make a program that calculates PI using the following algorithm: 我正在尝试制作一个使用以下算法来计算PI的程序:

PI = 4 x (1 - 1/3 + 1/5 - 1/7 + 1/9 ....) etc.

I don't understand how the operator placement is affecting the variable. 我不了解操作员的位置如何影响变量。

The outcome produced on the first iteration of the loop makes sense, but then it seems to reset to initialised value and ignore the assignment operators the second time through. 在循环的第一次迭代中产生的结果是有意义的,但随后似乎重置为初始化值,并且第二次忽略了赋值运算符。 Repeating this outcome 1, then outcome 2, then outcome 1, outcome 2, and so forth... 重复此结果1,然后是结果2,然后是结果1,结果2,依此类推...

I tried looking in this book called "Java: How to Program (Early Objects), 11th Edition" and in chapter four they went over operators but I could see that they didn't cover the conditions within a loop. 我尝试在名为“ Java:如何编程(早期对象),第11版”的书中进行浏览,在第四章中,他们跳过了运算符,但我发现它们没有涵盖循环中的条件。

   double k = 1.0, j = 1.0;

   double sum = 0, PI = 0;

   while((Math.abs(PI-Math.PI)>0.000001)){
       sum += k/j;

       j = j + 2;

       k=-k;

       PI = 4 * sum;    

       System.out.println(k);
    }   

I changed the conditions of the while loop to run 4 times and print k. 我将while循环的条件更改为运行4次并打印k。 I expected the first printing of the variable 'k' to be -1.0. 我希望变量“ k”的第一次打印为-1.0。 It is, but the second printing of k (second looping of while loop) is 1.0. 是,但是k的第二次打印(while循环的第二次循环)为1.0。 The third is -1.0, then 4th is 1.0, and so forth... 第三个是-1.0,然后第四个是1.0,依此类推...

I don't understand why it isn't -1.0 on all iterations, because with java assignment operators, as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k. 我不明白为什么在所有迭代中它都不是-1.0,因为据我所知,对于Java赋值运算符,如果左运算符为'=',而右运算符为增量或减量符号,那么结果应该始终是变量k总是= -k。

You initialize k outside the loop, so that only happens once. 您可以在循环外初始化k ,因此只能发生一次。 The initial value is 1 . 初始值为1

During each iteration you negate k : 在每次迭代期间,您求k

k=-k;
  • During the first iteration 1 is negated to become -1 . 在第一次迭代期间,将1取为-1
  • During the second iteration -1 is negated to become 1 . 在第二次迭代期间, -1被取反为1
  • During the third iteration 1 is negated to become -1 . 在第三次迭代中,将1取为-1
  • and so on 等等

as far as I know, if the left operator is '=' and the right operator is an incremental or decremental symbol, then the result should always be that the variable k will always = -k. 据我所知,如果左运算符为“ =”,而右运算符为递增或递减符号,则结果应始终为变量k始终为-k。

I think you are confusing the operation -= with the operation =- 我认为您将操作-=与操作=-混淆了

a -= b perform ab and store the result in a ab a -= b执行ab并将结果存储在a

a = -b perform -b (change sign) and then store -b into a a = -b执行-b (更改符号),然后将-b存储到a

That is what that k = -k does. 那就是k = -k作用。 Take the value of k , change its sign and store into k . k的值,更改其符号并将其存储为k That is equivalent to say that change the sign of k . 这相当于说改变k的符号。

Before loop 循环前

k = 1

  • loop #1 k = -(1.0) : so now k = -1.0 , it's carried -1.0 over to the next loop. 循环#1 k = -(1.0) :现在k = -1.0 ,它将-1.0传递到下一个循环。

  • loop #2 k = -(-1.0) : so now k = 1.0 , it's carried 1.0 over to the next loop. 循环#2 k = -(-1.0) :现在k = 1.0 ,将1.0传递到下一个循环。

  • loop #3 k = -(1.0) : so now k = -1.0 , it's carried -1.0 over to the next loop. 循环#3 k = -(1.0) :现在k = -1.0 ,它将-1.0传递到下一个循环。

  • and so on 等等

k is changing every loop, just like how j is not 3.0 every loop. k在改变每个循环,就像j在每个循环中不是3.0一样。

The incremental and decremental symbol you're talking about is probably k-- and k++ , or maybe k-=k and k+=k . 您正在讨论的增量和减量符号可能是k--k++ ,或者可能是k-=kk+=k I'm not sure. 我不确定。

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

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