为什么在for循环内部的while循环进入无限循环?

huangapple go评论103阅读模式
英文:

Why is while loop inside a for loop entering into an infinite loop?

问题

当我尝试运行这段代码时,它进入了一个无限循环,如果有人能解释一下,然后最后的条件语句永远不会被执行。我试图打印出从1到500的阿姆斯特朗数。如果有人能调试它并给我正确的代码,或者至少帮我了解这里发生了什么。

  1. while循环陷入无限循环
  2. for(int s=1; s<=500; s++)
  3. {
  4. //System.out.println(i);
  5. int total=0;
  6. while(s != 0) {
  7. //System.out.println(s);
  8. int digit = s % 10;
  9. //System.out.println(digit+" "+s);
  10. total = total+(digit*digit*digit);
  11. //System.out.println(total);
  12. //reversed = reversed*10 + digit;
  13. s = s/10;
  14. //System.out.println(s);
  15. }
  16. if (total==s) {
  17. System.out.println(s);
  18. }
  19. }
英文:

While loop entering into infinite loop.

  1. for(int s=1; s<=500; s++)
  2. {
  3. //System.out.println(i);
  4. int total=0;
  5. while(s != 0) {
  6. //System.out.println(s);
  7. int digit = s % 10;
  8. //System.out.println(digit+" "+s);
  9. total = total+(digit*digit*digit);
  10. //System.out.println(total);
  11. //reversed = reversed*10 + digit;
  12. s = s/10;
  13. //System.out.println(s);
  14. }
  15. if (total==s) {
  16. System.out.println(s);
  17. }
  18. }

When i try to run this it enters into an infinite loop, if anyone can explain it and then the last if never gets executed. I am trying to print armstrong numbers from 1 to 500. If anyone can debug it and give me the correct code or at least help me get to know what is going on here.

答案1

得分: 0

你正在在另一个循环内部修改循环变量 s。这就是你陷入无限循环的原因。在这里使用一个临时变量会更有意义。

修正后的代码:

  1. for (int s = 1; s <= 500; s++) {
  2. int total = 0;
  3. int temp = s; // 使用临时变量
  4. while (temp != 0) {
  5. int digit = temp % 10;
  6. total = total + (digit * digit * digit);
  7. temp = temp / 10;
  8. }
  9. if (total == s) {
  10. System.out.println(s);
  11. }
  12. }

在这个代码示例中,temp 在循环的每次迭代中保存了 s 的值。现在,它每次被除以 10 次,不会改变 s 的值。if 语句 if (total == s) 现在应该能够正常工作。

英文:

You are changing the variable s of the loop inside another loop.
This is the reason why you get an infinite loop. Using a temporary variable would make sense here.

Fixed Code:

  1. for (int s = 1; s &lt;= 500; s++) {
  2. int total = 0;
  3. int temp = s; // Use temp variable
  4. while (temp != 0) {
  5. int digit = temp % 10;
  6. total = total + (digit * digit * digit);
  7. temp = temp / 10;
  8. }
  9. if (total == s) {
  10. System.out.println(s);
  11. }
  12. }

In this example of code temp is holding the value of s for each iteration of the loop. Now it gets divided 10x every time leaving s unchanged. The if-clause if (total == s) should work correctly now.

答案2

得分: 0

你不应该改变循环变量 s:s=s/10。请使用临时变量代替:

  1. for (int s = 1; s <= 500; s++) {
  2. int total = 0;
  3. int q = s;
  4. while (q != 0) {
  5. int digit = q % 10;
  6. total = total + (digit * digit * digit);
  7. q = q / 10;
  8. }
  9. if (total == s) {
  10. System.out.println(s);
  11. }
  12. }
英文:

You should not change the cycle variable s: s=s/10. Use the temporary variable instead:

  1. for(int s=1; s&lt;=500; s++)
  2. {
  3. //System.out.println(i);
  4. int total=0; int q = s;
  5. while(q != 0) {
  6. //System.out.println(s);
  7. int digit = q % 10;
  8. //System.out.println(digit+&quot; &quot;+q);
  9. total = total+(digit*digit*digit);
  10. //System.out.println(total);
  11. //reversed = reversed*10 + digit;
  12. q = q/10;
  13. //System.out.println(s);
  14. }
  15. if (total==s) {
  16. System.out.println(s);
  17. }
  18. }

答案3

得分: 0

我相信while循环不会无限循环,因为s/10会返回0(请记住这都是整数,而在整数中1/10 = 0)。

然而,for循环将会无限循环,因为每次通过while循环时,您都将s设置为0,然后for循环中的指令会将其增加1,因此s在每次for循环的迭代中都变为1

至于if语句,它不会执行,因为在您的代码中total = total+(digit*digit*digit);其中digit = s % 10导致1,因此在if条件中您将得到一个条件1 == 0,这将始终评估为false

英文:

I believe while loop will not be infinite because s/10 will return 0 (keep in mind it's all integer and in integer 1/10 = 0).

The for loop however will be infinite because each time you run through the while loop you set s to 0 and then the instruction in the for loop increments it by 1, so s ends up being 1 in every iteration of the for loop.

As for the if statement, it does not execute because in your code total = total+(digit*digit*digit); where digit = s % 10 that results to 1, hence in the if condition you'll end up with a condition 1 == 0 that will always evaluate to false

huangapple
  • 本文由 发表于 2023年5月29日 06:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定