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

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

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

问题

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

while循环陷入无限循环

for(int s=1; s<=500; s++)
{  
    //System.out.println(i);
    int total=0;
    while(s != 0) {
        //System.out.println(s);
        int digit = s % 10;
        //System.out.println(digit+" "+s);
        total = total+(digit*digit*digit);
        //System.out.println(total);
        //reversed = reversed*10 + digit;
        s = s/10;
        //System.out.println(s);
    }
    if (total==s) {
        System.out.println(s);
    }
}
英文:

While loop entering into infinite loop.

	for(int s=1; s<=500; s++)
	{  
		//System.out.println(i);
		int total=0;
		while(s != 0) {
			//System.out.println(s);
			int digit = s % 10;
			//System.out.println(digit+" "+s);
			total = total+(digit*digit*digit);
			//System.out.println(total);
			//reversed = reversed*10 + digit;
			s = s/10;
			//System.out.println(s);
			}
		if (total==s) {
			System.out.println(s);
		}
	}

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。这就是你陷入无限循环的原因。在这里使用一个临时变量会更有意义。

修正后的代码:

for (int s = 1; s <= 500; s++) {
    
  int total = 0;
  int temp = s; // 使用临时变量
  
  while (temp != 0) {
    int digit = temp % 10;
    total = total + (digit * digit * digit);
    temp = temp / 10;
  }
  
  if (total == s) {
    System.out.println(s);
  }
}

在这个代码示例中,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:

for (int s = 1; s &lt;= 500; s++) {
    
  int total = 0;
  int temp = s; // Use temp variable 
  
  while (temp != 0) {
    int digit = temp % 10;
    total = total + (digit * digit * digit);
    temp = temp / 10;
  }
  
  if (total == s) {
    System.out.println(s);
  }
}

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。请使用临时变量代替:

for (int s = 1; s <= 500; s++) {  
    int total = 0; 
    int q = s;
    while (q != 0) {
        int digit = q % 10;
        total = total + (digit * digit * digit);
        q = q / 10;
    }
    if (total == s) {
        System.out.println(s);
    }
}
英文:

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

for(int s=1; s&lt;=500; s++)
{  
    //System.out.println(i);
    int total=0; int q = s;
    while(q != 0) {
        //System.out.println(s);
        int digit = q % 10;
        //System.out.println(digit+&quot; &quot;+q);
        total = total+(digit*digit*digit);
        //System.out.println(total);
        //reversed = reversed*10 + digit;
        q = q/10;
        //System.out.println(s);
        }
    if (total==s) {
        System.out.println(s);
    }
}

答案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:

确定