黑马程序员技术交流社区

标题: 求1-200素数及素数个数方法,自己想的方法 [打印本页]

作者: 灵魂灬丿丶轩    时间: 2016-3-25 00:27
标题: 求1-200素数及素数个数方法,自己想的方法
//1到200的素数及个数,我是新手,大神多多指教!!!
class Test1 {
        public static void main(String[] args) {
                int i;
                int count = 0;
                int count1;
                for(i= 2;i <= 200;i++) {                       
                        count1 = 0;
                        for (int j = 1;j <=i ;j++ ) {
                                if (i % j == 0) {
                                        count1++;
                                }
                        }
                        if (count1 == 2 ) {
                                count++;
                                System.out.println("素数是:" + i);
                        }
                }
                System.out.println("素数的个数是:" + count);
        }
}


程序执行结果.png (79.16 KB, 下载次数: 13)

程序执行结果

程序执行结果

作者: 风二中kyf    时间: 2016-3-25 00:32
66666666666666
作者: freshnboy    时间: 2016-3-25 01:02
这样还是不够精简,视频上写代码的尿性会使用break更多一些




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2