【发布时间】:2011-09-08 16:26:34
【问题描述】:
为什么我的程序会跳过 while 循环?
class DigitRepeat
{
public static void main(String args[])
{
int n=133,dig=3,digit,count=0;
while(n!=0)
{
digit=n%10;
if(dig==digit)
{
count=count++;
}
n=n/10;
}
if(count==0)
System.out.println("digit not found");
else
System.out.println("digit occurs "+count+" times.");
}
}
【问题讨论】:
-
现在是使用调试器自己解决问题的好时机。我怀疑while循环正在执行,问题不是你想的那样。
-
那么你的问题是什么?如果你在问题中添加了这个程序的输出和你想要的输出,你可能会在这里得到更好的帮助。
-
你的结果是什么? - “找不到数字”?
-
我不确定这是否能解决你的问题,但
count++;就足够了,你不必写count=count++;。 -
事实上
count = count++;是罪魁祸首,因为在这种情况下 count 始终保持为 0。
标签: java