program logic of printing the prime numbers

前端 未结 7 567
别那么骄傲
别那么骄傲 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:35

    A quick but dirty solution..

    import java.util.Scanner;
    
    class testing
    {
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);
            int n, i, j, count = 1;
            System.out.print("How many Numbers ? : ");
            n = input.nextInt();
            for(j = 1;count<=n;j++)
            {
    
                if(j==1 || j==2)
                {
                    System.out.println(j);
                    count++;
                    continue;
                }
                for(i=2;i<=j/2;i++)
                {
                    if(j%i==0)
                        break;
                    else if(i == j/2 && j%i != 0)
                    {
                        System.out.println(j);
                        count++;
                    }  
                }
            }       
        }
    }
    

提交回复
热议问题