2012-11-19 2 views
0

testPerfectgetPerfect에서 숫자를 받고 배열에 숫자를 넣어야합니다. 현재 배열 인쇄가 있지만 모두 0입니다. 숫자의 요소를 배열에 넣으려는 제안이 있습니까?배열에 숫자를 넣는 방법

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; 


} 






} 
+0

일부 엉망입니다. 'testPerfect()'는 절대로 호출되지 않습니다. 그리고 당신은'0..0'의 배열을 얻고 있습니다. 메인 안에 새로운 배열을 만들고 인쇄했기 때문입니다. –

+0

코드를 살펴보면 배열을 선언하고 배열 자체를 인쇄하는 것입니다. 읽은 완벽한 번호를 배열에 저장하지 않습니다. 완벽한 숫자를 저장 한 다음 배열을 인쇄하십시오. 그리고 어디에서 testPerfect()를 호출 했습니까? –

답변

1

어쩌면 그럴 것 같은가요? 먼저 으로 을 getPerfect()으로 받고 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; 


} 






} 
+0

@ user1834819, Wc –

관련 문제