java - 将数字因式分解放入数组中

标签 java arrays methods

方法 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;


}






}

最佳答案

也许是这样的? 首先,您需要使用从 getPerfect() 接收的 gParray 从主函数调用 testPerfect() > 作为参数。 其次,您需要删除调用 testPerfect(),它是方法 testPerfect() 的第一行,否则这将导致无限递归调用。

public class aermel_Perfect
{
public static void main ( String args [] )
{
    int gN;
    int gP = getPerfect();
    int [] array = new int[100];
    array=testPerfect(gP,array);
    //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;


}






}

关于java - 将数字因式分解放入数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449509/

相关文章:

objective-c - 子类调用父类的方法

java - Spring MVC - 动态更改安全设置

java - 从泛型类返回一个数组。如何在 main 方法中打印数组中的数据?

java - 事件基础编程

java - 使用类图在java中实现聚合和组合

java - JUnit 测试方法失败

c - 你将如何在 obj-c 中定义一个简单的 "min"方法

javascript - 简单图像测验困惑 : Next Button Triggers a New Full Screen Image

php - 代码点火器/PHP : How to merge distinct arrays and make it unique into one array in query result

c - 在不覆盖指针指向的值的情况下取消 C 中结构数组的索引