program logic of printing the prime numbers

前端 未结 7 572
别那么骄傲
别那么骄傲 2021-01-25 22:15

Can any body help to understand this java program?

It just prints prime numbers, as you enter how many you want and it works well.

class PrimeNumbers
{           


        
7条回答
  •  遥遥无期
    2021-01-25 22:16

    public class PrimeNumber {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList a = new ArrayList();
        for (int i = 1; i <= 100; ++i) {
            if (isPrime(i))
                a.add(i);
        }
        System.out.println("List : " + a);
    
    }
    
    public static boolean isPrime(int value) {
        if (value <= 1)
            return false;
    
    
        if ((value % 2) == 0)
            return (value == 2);
    
        for (int i = 3; i <= value - 1; i++) {
            if (value % i == 0) {
                return false;             
            }
        }     
    
        return true;
    }
    
    }
    

提交回复
热议问题