클래스는 방금 함수 및 메서드가 포함 된 형식으로 코드를 작성하는 방법을 배우기 시작했으며 이것이 처음 시도한 것입니다. 나는 혼란스러워합니다.함수/메서드 및 배열
할당은 임의로 선택된 숫자에 1의 풀을 만들고 배열에 풀을 저장하는 것입니다. 그런 다음 풀을 인쇄하십시오. 사용자는 풀에서 번호를 선택하고 프로그램은 그 번호의 제수를 찾아 인쇄 할 다른 배열에 저장합니다.
제수를 얻고 새로운 배열에 저장 한 다음 배열을 인쇄하는 데 문제가 있습니다.
import java.util.Scanner;
import java.util.Random;
public class GetDivisors {
public static void main (String[] args){
Scanner read = new Scanner(System.in);
int []pool = new int[100];
int []divisors = new int[100];
int size;
int pick;
int numDivisor;
// Program Heading
printProgramHeading();
// Input and test
size = createPool(pool);
printPool(pool, size);
System.out.println();
System.out.println();
System.out.println("Enter a non-prime value listed in the pool above: ");
pick=read.nextInt();
// Data Processing
numDivisor = getDivisors(pool, divisors, pick);
// Output Section
printDivisors(divisors, size, pick);
} // end main
// Function and Method Specifications
// Name : printProgramHeading
// Description : This method prints the program heading to the monitor in all caps and with a dividing line
// : followed by a blank line.
// Parameters : None.
// Return : None.
public static void printProgramHeading() {
System.out.println("\tGET DIVISORS");
System.out.println(" ************************");
System.out.println();
} // end printHeading
//Name : createPool
//Description : This funtion generates an array of consecutive integers from 1 to a randomly generated
// : number no greater than 100.
//Parameters : An integer array.
//Return : An integer (the randomly generated number), representing the size of the array.
public static int createPool(int[]pool) {
Scanner read = new Scanner(System.in);
Random random = new Random();
int size=0;
size=random.nextInt(100)+1;
pool=new int[size];
return(size);
} // end createPool
//Name : printPool
//Description : This method prints the pool of numbers to the monitor no more than 10 per line.
//Parameters : The pool array, and the size of the pool in that order.
//Return : None.
public static void printPool(int[] pool, int size) {
int index;
int count=0;
for(index=1; index<size; index++){
System.out.print(pool[index]=index);
System.out.print(" ");
count++;
if(count == 10){
System.out.println();
count=0;
} // end if loop
} // end for loop
} // end printPool
//Name : getDivisors
//Description : This funtion stores all the divisors of the user's pick into the divisor array.
//Parameters : The pool array, the divisor array, and the user's pic, in that order.
//Return : The number of divisors found.
public static int getDivisors(int[] pool, int[] divisors, int pick){
int numDivisors = 0;
int index = 0;
for(index=1; index <= pick; index++){
if(pick % index == 0){
numDivisors++;
divisors[index] = index;
} // end if loop
} // end for loop
return(numDivisors);
} // end getDivisors
//Name : printDivisors
//Description : This method prints the contents of the divisors array to the monitor all on one line with
// : a leading label.
//Parameters : The divisor array, an integer representing the number of divisors. and the user's pick
// : in that order.
//Return : None.
public static void printDivisors(int[] divisors, int size, int pick){
int index = 0;
System.out.println("The divisors of " + pick + ": " + divisors[index] + " ");
} // end printDivisors
} // end class
고맙습니다!
'getDivisors()'의'for' 루프가 정확히 무엇을 기대하는지 기술하십시오. –
약수를 얻으려면 풀 시작 부분에서 시작하여 사용자가 선택한 번호로 이동하십시오. 풀 번호로 나눈 값의 수가 0 인 경우 풀 번호는 제수입니다. 그런 다음 다음 단계에서 인쇄 할 수있는 배열에 저장해야합니다. 프로그램을 실행하고 약수를 인쇄하려고하면 약수 목록 대신 0이 생깁니다. – kp03
새로 생성 된 배열 ('divisors')의 어떤 요소에도 절대 값을 할당하지 않습니다. –