这个程序应该接受用户输入并确定它是否是一个完美的数字。当我尝试编译它时,我得到错误 Method testPerfect in class scalvert_Perfect cannot be applied to given types;
- 测试完美(数字);
- 必需:int,int
- 发现:int
- 原因:实际参数列表和形式参数列表的长度不同
我的代码:
import java.util.Scanner;
public class scalvert_Perfect
{
public static void main ( String args[] )
{
Scanner input = new Scanner(System.in);
int test;
int num = 0;
int counter = 0;
do
{
System.out.print("How many numbers would you like to test? ");
test = input.nextInt();
}while(test < 1);
do
{
System.out.print("Please enter a possible perfect number: ");
num = input.nextInt();
testPerfect(num);
printFactors(num);
counter++;
}while(counter < test);
}
public static boolean testPerfect(int num, int test)
{
int sum = 0;
for(int i = 0; i < test ; i++)
{
if(num % i == 0)
{
sum += i;
}
}
if(sum == num)
{
return true;
}
else
{
return false;
}
}
public static void printFactors(int num)
{
int x;
int sum = 0;
for(int factor = num - 1 ; factor > 0; factor--)
{
x = num % factor;
if (x == 0)
{
sum = sum+factor;
}
}
if(sum != num)
{
System.out.printf("%d:NOT PERFECT",num);
}
if(sum == num)
{
System.out.printf("%d: ",num);
for(int factor=1; factor < num; factor++)
{
x = num % factor;
if(x == 0)
{
System.out.printf("%d ",factor);
}
}
}
System.out.print("\n");
sum = 0;
}
}