gpt4 book ai didi

java - 寻找一个奇完全数

转载 作者:行者123 更新时间:2023-11-30 04:10:40 25 4
gpt4 key购买 nike

我编写了这两个方法来确定一个数字是否完美。我的教授希望我将它们结合起来,看看是否存在奇完全数。我知道没有(已知的),但我需要实际编写代码来证明这一点。

问题出在我的主要方法上。我测试了两种测试方法。我尝试调试,结果卡在数字 5 上,但我不明白为什么。这是我的代码:

public class Lab6
{
public static void main (String[]args)
{
int testNum = 3;

while (testNum != sum_of_divisors(testNum) && testNum%2 != 2)
testNum++;

}

public static int sum_of_divisors(int numDiv)
{
int count = 1;
int totalDivisors = 0;

while (count < numDiv)
if (numDiv%count == 0)
{
totalDivisors = totalDivisors + count;
count++;
}
else
count++;

return totalDivisors;
}

public static boolean is_perfect(int numPerfect)
{
int count = 1;
int totalPerfect = 0;

while (totalPerfect < numPerfect)
{
totalPerfect = totalPerfect + count;
count++;
}
if (numPerfect == totalPerfect)
return true;
else
return false;
}
}

最佳答案

制作

testNum%2 != 2

作为

testNum%2 != 0

关于java - 寻找一个奇完全数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com