Основа моей проблемы здесь: 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
}
}
При использовании глобальных переменных абстракция не применяется (но может).
Основной метод запускает программу, затем отслеживает метод printPrimeNumbers, а затем метод boolean isPrime.Я хочу вернуть выходные данные этого метода в новый массив ...
Размер массива будет определяться пользовательским вводом <"Сколько простых чисел вы хотите работать?">, А затем<"С какого числа вы хотите начать?>
Проблема, похоже, я не могу передать вывод метода в элементы массива.
Мысли?