printing out prime numbers from array

前端 未结 2 1181
刺人心
刺人心 2021-01-28 08:19

I\'d like to print out all prime numbers from an array with method. I can do it with one int but don\'t know how to return certain numbers from array. Thanks for help!



        
相关标签:
2条回答
  • 2021-01-28 08:35

    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.)

    0 讨论(0)
  • 2021-01-28 08:46

    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.

    0 讨论(0)
提交回复
热议问题