A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. The method generatePrimes places the first set of primes in an array. Add the statements to the morePrimes method that fill the remaining locations in the array with the next primes. For each value, divide the value by all of the primes in the array; if the value is not evenly divisible by one of the primes, then that value is a prime and is to be added to the array in the next available location. Do not use any temporary arrays. This question does not require the use of the Sieve of Eratosthenes. The method is to terminate when all of the locations in the array are filled with prime values. Print the values in the array to System.out when the loop terminates.


  2. import java.util.Arrays;

  3. public class primeNumber
  4. {
  5. public static void main(String[] parms)
  6. {
  7. process();
  8. System.out.println("\nProgram completed normally.");
  9. }

  10. public static void process()
  11. {
  12. int[] primes;
  13. int numPrimes;

  14. primes = new int[25];
  15. numPrimes = generatePrimes(primes);
  16. System.out.println(Arrays.toString(primes));

  17. morePrimes(primes, numPrimes);
  18. System.out.println(Arrays.toString(primes));
  19. }

  20. public static int generatePrimes(int[] primes)
  21. {
  22. int[] initialPrimes = new int[]{2, 3, 5, 7, 11, 13, 17};
  23. int count;

  24. System.arraycopy(initialPrimes,0,primes,0,initialPrimes.length);

  25. return initialPrimes.length;
  26. }

  27. public static void morePrimes(int[] primes, int numPrimes)
  28. {
  29. int count;
  30. int value;
  31. boolean done;

  32. }
  33. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马