0
方法testPerfect
应该从getPerfect
接收一个数字,将数字因子分解并将数字放入数组中。 我目前有阵列打印,但全部为零。任何建议将数字的因素放入数组中?将因子分解为数组
public class aermel_Perfect
{
public static void main (String args [])
{
int gN;
int gP = getPerfect();
int [] array = new int[100];
//int printFactors;
System.out.println(Arrays.toString(array));
}
public static int getNum() //Get amount of numbers to check
{
Scanner input = new Scanner (System.in);
System.out.print("How many numbers would you like to test? ");
int count = input.nextInt();
int perfect = 1;
boolean vN = validateNum(count, perfect);
while(!vN)
{
System.out.print (" How many numbers would you like to test? ");
count = input.nextInt();
vN = validateNum(count, perfect);
}
return count;
}
public static boolean validateNum(int count, int perfect ) //Check if number is valid
{
if ((count <= 0) || (perfect <= 0))
{
System.out.print("Non-positive numbers are not allowed.\n");
}
else
{
return true;
}
return false;
}
public static int getPerfect() //Gets the numbers to test
{
Scanner input = new Scanner (System.in);
int perfect = -1;
int count = getNum();
System.out.print("Please enter a perfect number: ");
perfect = input.nextInt();
boolean vN = validateNum(perfect, count);
while (!vN)
{
System.out.print("Please enter a perfect number: ");
perfect = input.nextInt();
vN=validateNum(perfect, count);
}
return perfect;
}
public static int[] testPerfect(int perfect, int[] array)
{
testPerfect(perfect, array);
int limit = (int) Math.ceil(Math.sqrt(perfect));
int index = 0;
for (int i = 1; i <=limit; i++)
{
array[index++] = i;
perfect /= i;
}
array[index] = perfect;
return array;
}
}
这是一些混乱。你的'testPerfect()'永远不会被调用。而且你得到了一个'0..0'的数组,因为在你的main中你刚刚创建了一个新的数组并且打印了它。 –
正如我经历了你的代码,你只是声明数组,并打印数组本身。您不会将读取的完美数字存储到数组中。存储您的完美数字,然后打印数组。你在哪里叫testPerfect(); –