【问题标题】:program to print series of prime numbers using java使用java打印一系列素数的程序
【发布时间】:2015-12-16 08:46:37
【问题描述】:

这段代码是打印一系列素数直到给定的限制,但是当我试图执行这个时,它进入了无限循环。

import java.io.*;
class a
{
    public static void main(String s[]) throws IOException
    {
        int count=1;
        String st;
        System.out.println("how many prime no. do you want");
        BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
        st=obj.readLine();
        int n=Integer.parseInt(st);
        while(count!=n)
        {
            int num=2;
            for(int i=2;i<num;i++)
            {
                if(num%i==0)
                {
                    count++;
                    break;
                }
            }
            num++;
        }
    }
}

【问题讨论】:

  • 你没有打印任何东西?
  • 您在 while 循环的每次迭代开始时将 num 设置为 2,而您的 for 循环在 2 处开始 i。这将始终使 i &lt; num 条件失败,导致它从不跑。您也可以从 1 开始 count,这意味着您也不能要求一个素数。
  • 除了上面的 cmets:你实际上是在计算非素数......
  • 您的问题标题与您的代码无关。
  • 您没有在内部循环中迭代到 num。 sqrt(num) 会做。

标签: java primes


【解决方案1】:

在循环中使用 isPrime(int num) 方法生成素数示例如下代码所示

public class PrimeNumbersSeries 
{
    public boolean isPrime(int num)
    {
        boolean flag=true;
        for(int i=2; i<=num/2; i++)
        {
            if(num%i==0)
            {
                flag=false;
            }
            else 
           {
               flag=true;
           }
        }
        if(num<=1)
        {
            flag=false;
        }
        return flag;
    }
    public static void main(String []args)
    {
        System.out.println("how many prime no. do you want");
        PrimeNumbersSeries prime=new PrimeNumbersSeries();
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        int count =0;
        int j=1;
        while(count!=num)
        {
            if(prime.isPrime(j))
            {
                System.out.print(j+", ");
                count++;
            }            
            j++;
       }
   }
}

【讨论】:

    【解决方案2】:

    问题是 num 的值在循环开始时始终为 2,即使您再次说 num++ 它也需要 num=2 这是开始语句并且永远不会进入 for 循环,因此是无限循环。这将起作用

    int num=2;
    while(count!=n)  {         
       for(int i=2;i<num;i++) {
         if(num%i==0) {
              count++;
              break;
         }
       }
       num++;
    }
    

    【讨论】:

    • 无需对 num 应用循环,甚至可以执行 for(int i=2; i
    【解决方案3】:

    这是你的工作代码:

    import java.io.*;
        class A
        {
            public static void main(String s[]) throws IOException
            {
                int count=2;
                String st;
                System.out.println("how many prime no. do you want");
                BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
                st=obj.readLine();
                boolean isPrime = false;
    
                int n= Integer.parseInt(st);
                int num=3;
                if(n>=1){
                    System.out.println(2);
                }
                while(count<=n)
                {
                    //No need to go up to num. Up to sqrt(num) will do.
                    for(int i=2;i<=Math.sqrt(num);i++)
                    {
                        if(num%i==0)
                        {
                            isPrime = false;
                            break;
                        }
                    }
                    if(isPrime){
                        System.out.println(num);// Added the print
                        count++;
                    }
                    isPrime = true;
                    num++;
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      修正后我得到了一系列素数。

      import java.io.*;
      class a
      {
          public static void main(String s[]) throws IOException
          {
              int count=1,count1=0;
              String st;
              System.out.println("how many prime no. do you want");
              BufferedReader obj= new BufferedReader (new InputStreamReader (System.in));
              st=obj.readLine();
              int n=Integer.parseInt(st);
              int num=2;
              while(count<=n)
              {
                  count1=0;
                  for(int i=2;i<num;i++)
                  {
                      if(num%i==0)
                      {
                          count1++;
                      }
                  }
                  if(count1==0)
                      {
                          System.out.println(num);
                          count++;
                      }
                  num++;
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        您可以查看此代码。

        import java.io.*;
        public class Main {
        
             public static void main(String[] args) throws IOException {
        
                BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
                //How many prime numbers you want to print?
                String s = bf.readLine();
                int n = Integer.parseInt(s);
                int count = 0;
                boolean loop = true;
                for(int i =2 ; loop ; i++){
        
                    if(isPrime(i))
                        {
                        System.out.println(i + " ");
                        count++;
                        }
                    if(count == n)
                        loop = false;
                }
            }
        

        下面的 isPrime() 方法检查一个数是否为素数。如果数字是素数,则返回 true,否则返回 false。

            public static boolean isPrime(int num) {
                boolean prime = true;
                for(int i=2 ; i<= Math.sqrt(num);){
                    if(num % i ==  0)
                        {
                            prime = false;
                            break;
                        }
                    if(i >= 3)
                    /* when i>=3 we do not need to check for every number. 
                      For avoiding even numbers i is incremented by 2. 
                      It reduces the number of looping */ 
                        i+=2;
                    else
                        i++;
                }
        
                return prime;
        
            }
        }
        

        【讨论】:

          【解决方案6】:
          import java.util.*;
          class Prime
          {
            public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
          
              System.out.println("enter  the  no of prime nos want:  ");
              int n2 = sc.nextInt();
              int flag = 1 ;
              int count = 0 ;
              for (int i =2; i<99999;i++ )
              {
                   for (int j=2; j<i;j++ )
                   {
                    if (i%j == 0)
                    {
                       flag = 0;
                       break;
                    }
                    else
                    {
                      flag =1;
                    }
                   }
          
                if (flag == 1)
                {
                    System.out.print(i +"\t");
                    count++ ;
                }
                if (count == n2)
                {
                    break  ;
                }
              }
            }
          }
          

          【讨论】:

            猜你喜欢
            • 2021-04-09
            • 2012-07-22
            • 2018-01-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-04
            相关资源
            最近更新 更多