随机生成100个1000以内的整数,将这100个整数中的素数找出来并写到一个文本文件中。
要求:实现两个类,一个为求素数类,另一个为主类,调用求素数类的方法。

解决方案 »

  1.   

    用Random()*1000+1生成1000以内的随机数
    用循环就能生成100个
    int x;
    int count=0;
    for(int i=1;i<=x;i++){
        if(x%i==0)
            count++;
    }if(count==2)
        System.out.println(x+"为素数");
    再用循环,判断100个数
      

  2.   

    import java.io.*;
    import java.util.*;public class YourTask
    {
    private static boolean isPrime( int n )
    {
        // fill in yourself
    }public static void work( long seed, String fileName )
    {
      PrintWriter writer = new PrintWriter( fileName );
      Random r = new Rondom( seed );
      int n = 0;
      try {
      for( int i = 0; i < 100; i++ ) {
        n = r.nextInt();
        if( isPrime( n ) ) 
           writer.println( n );
      }
       } catch( IOException ioe ) { 
         ioe.printStackTrace();
       }
       writer.close();
    }
    }