英文:
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 <= 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<=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+" "+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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论