将数字放入数组中

时间:2012-11-19 07:40:43

标签: 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;


}






}

1 个答案:

答案 0 :(得分:1)

这样的事可能吗? 首先,您需要使用从testPerfect()收到的gPgetPerfect()作为参数,从您的主电话拨打array。 其次,您需要删除调用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;


}






}