如何将方法输出传递给数组元素?

时间:2019-02-18 15:35:34

标签: java arrays methods

我的问题的基础在这里:https://github.com/experiencethebridge1/primeGap

最重要的是,我想创建一个数组,其中方法的输出将填充新数组的元素。

这不是家庭作业。

package primenumbermethod;

import java.util.Scanner;

public class PrimeNumberMethod {

    public static void main(String[] args) {
        System.out.print("How many prime numbers do you want to work with? ");
        Scanner input = new Scanner(System.in);
        int arraySize = input.nextInt();

        // Invoke printPrimeNumbers method
        System.out.println("If I can ever get it to work, the number of the "
                + "elements in the array I want to build will be " + arraySize +".");

        System.out.println();
        printPrimeNumbers(arraySize);

        // How can I read parts of a method into elements of an array?
        int[] myList = new int[arraySize];


    }

    public static int printPrimeNumbers(int numberOfPrimes) {
        final int NUMBER_OF_PRIMES_PER_LINE = 10;  // Display 10 per line
        Scanner input = new Scanner(System.in);
        System.out.print("What number do you want to start from?  ");
        int number = input.nextInt();
        int count = 0; // Count the number of prime numbers
        // Repeatedly find prime numbers
        while (count < numberOfPrimes) {
            // Print the prime number and increase the count
            if (isPrime(number)) {
                count++; // Increase the count
                if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.printf("%-15d\n", number);
                } else {
                    System.out.printf("%-15d", number);
                }
            }
            number++;
        }
        return 0;
    }
    // Method for checking if number is prime
    public static boolean isPrime(int number) {
        for (int divisor = 2; divisor <= number / 2; divisor++) {
            if (number % divisor == 0) {// If true, number is not prime 
                return false;  // Number is not a prime    
            }
        }
        return true; // Number is prime
    }
}

尝试使用全局变量,抽象不适用(但可以)。

main方法将启动程序,然后跟踪到方法printPrimeNumbers,然后跟踪到方法boolean isPrime。我想将该方法的输出返回到新数组中...

数组大小将由用户输入<“您要使用多少个质数?”>,然后<<您想从哪个数字开始?>

问题,我似乎无法将方法的输出传递给数组的元素。

有想法吗?

1 个答案:

答案 0 :(得分:1)

我建议您以以下方式重组代码:

public static void main(String[] args) {
    int numberOfPrimes = readIntFromCommandLine...;
    int numberToStartWith = readIntFromCommandLine...;

    int[] primeNumbers = getPrimeNumbers(numberOfPrimes, numberToStartWith);

    // maybe extract this to another method as well
    for (int prime : primeNumbers) {
        // do whatever you want with prime, e.g. print it - or sum it, or multiply or whatever
    }
}

public static int[] getPrimeNumbers(int amount, int from) {
    int[] primes = new int[amount];
    int count = 0;

    /* now put your current prime logic here and whenever you 
       find a prime set primes[count] = newlyFoundPrime;  */
}

public static boolean isPrime(int number) { /* stays the same */ }

通常最好仅在代码中定义明确的位置而不是在整个位置请求用户输入。因此,我将两个输入置于前面。另一个通常的好主意是使每种方法(也许main方法除外)仅做一件事情。您的isPrime是一个很好的例子。将打印逻辑移出getPrimeNumbers可以简化该方法,并让您在另一个专用的地方进行打印。