printing out prime numbers from array

可紊 提交于 2019-12-02 08:06:26

I would suggest you separate this into two methods:

  • One method to determine whether a single number is prime
  • One method to iterate through an array, call the first method with each number, and print out the values for which the method returns true.

That separates out the two concerns neatly. If you're stuck on exactly how to do this, please give details of which bit you find hard. (I'm assuming this is homework, which is why I haven't just included the code.)

Assuming you have:

  • One array of integers, with some being prime and some being not prime.
  • A function for testing if one of those numbers is prime.

Simply iterate over the array, and for each number:

if (isPrime(n)) {
    system.out.println(n);
}

You probably don't want to try and do multiple ints at once, one at a time should be alot simpler to code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!