• Java经典编程题50道之二十七


    求100之内的素数。

    public class Example27 {
        public static void main(String[] args) {
            prime();
        }

        public static void prime() {
            System.out.print(2 + " ");
            System.out.print(3 + " ");
            int count = 2;
            boolean flag = false;
            for (int i = 2; i <= 100; i++) {

                for (int j = 2; j <= Math.sqrt(i); j++) {
                    if (i % j == 0) {
                        flag = false;
                        break;
                    } else {
                        flag = true;
                    }
                }
                if (flag) {
                    count++;
                    System.out.print(i + " ");
                    if (count % 10 == 0) {
                        System.out.println();
                    }
                }
            }
            System.out.println(" 共有" + count + "个素数。");
        }
    }

  • 相关阅读:
    Java多线程在JavaWeb中的应用
    hibernate,spring,struts的流程以及使用理由
    hibernate如何实现持久化
    ibatis与hibernate有什么区别
    关于导出pdf的例子
    BusyBox
    Android中如何查看内存
    使用PopupWindow实现Menu功能
    ScrollView原理
    eclipse 代码提示时闪退问题
  • 原文地址:https://www.cnblogs.com/qubo520/p/6950804.html
Copyright © 2020-2023  润新知