想知道,求前50个素数,哪里出问题,呜呜(┯_┯),能运行,但是没有任何结果, scanner前面的

想知道,求前50个素数,哪里出问题,呜呜(┯_┯),能运行,但是没有任何结果, scanner前面的那些省略了 Scanner in = new Scanner(System.in); // int n = in.nextInt(); int number = 1; //计数器 do { OUT: for(int n = 2;n<10000;n++) { for(int i = 2; i

代码没贴全呀。贴 代码段 更容易读

 

我看到的是这样的:

你贴的代码没法看,不知道那些内容是注释。用 代码段 贴代码吧。

你的代码我整理了以下,用代码段贴是这样的:

        Scanner in = new Scanner(System.in); 
        // int n = in.nextInt(); 
        int number = 1; //计数器 
        do { 
            OUT: 
            for(int n = 2;n<10000;n++) 
            { 
                for(int i = 2; i<n; i++) 
                { 
                    if(n%i == 0) 
                    { 
                        System.out.print(n+" "); 
                    } 
                    else 
                    { 
                    }
                    ++number; 
                } 
                if(number == 50) 
                { 
                    break OUT; 
                } 
            } 
       }while(number<=50);
    }
}

逻辑错了,能整除就不是素数,搞反了。

我修改了一下:


import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in); 
        int count = 0; 
        for(int n = 2; count < 50; n++) 
        { 
            boolean isPrime = true;
            for(int i = 2; i<n; i++) 
            { 
                if(n%i == 0) 
                { 
                    isPrime = false;
                    break; 
                } 
            } 
            
            if(isPrime) 
            {
                ++count;
                System.out.print(n+" "); 
            } 
        }
    }
}


//Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229

 

这样太麻烦了,