我正在尝试让程序计算前四个完美数字。它编译,但是当我运行程序时,for循环似乎是不可见的。就像我查看调试器一样,看起来只是跳过for循环并且变量不存在。
public static void main(String[] args)
{
int pNum = 2; // starting number to check
int sum = 0;
for (int count = 1; count <= 4; count++)
{
for (int factor = 1; factor < pNum; factor++)
{
if (pNum % factor == 0)
sum += factor;
}
if (pNum == sum)
{
System.out.println(pNum + " is a perfect number.");
count++;
}
pNum++; sum = 0;
}
}
答案 0 :(得分:0)
我认为您的调试器正在运行,尝试使用printoutput来检查循环是否正常工作
for(int count = 1; count <= 4; count++){
System.out.println("count:"+count);
答案 1 :(得分:0)
count = 1, pNum = 2, sum = 0
factor = 1, sum = 0
count = 2, pNum = 3, sum = 0
factor = 1, sum = 0
factor = 2, sum = 1
count = 3, pNum = 4, sum = 0
factor = 1, sum = 0
factor = 2, sum = 1
factor = 3, sum = 3
count = 4, pNum = 5, sum = 0
factor = 1, sum = 0
factor = 2, sum = 1
factor = 3, sum = 1
factor = 4, sum = 1