首页 > 其他 > 详细

求质数

时间:2015-06-18 22:03:38      阅读:229      评论:0      收藏:0      [点我收藏+]

1.输入被求质数的数

2.输入一行输出质数的个数

3.输入是否继续运算

import java.util.Scanner;

public class 质数 {

    public static void main(String[] args) {
        boolean isPro = true;
        while (isPro) {
            System.out.print("请输入一个数:");
            @SuppressWarnings("resource")
            int num = (new Scanner(System.in)).nextInt();
            System.out.print("一行输出几个质数:");
            int outNum = (new Scanner(System.in)).nextInt();
            int count = 0;
            for (int i = 101; i < num; i += 2) {
                boolean isOrNot = true;
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        isOrNot = false;
                        break;
                    }
                }
                if (isOrNot) {
                    if (count % outNum == 0 && count / outNum != 0)
                        System.out.println("");
                    System.out.print(i + " ");
                    count++;
                }
            }
            System.out.println("\n是否继续?(y/n)");
            isPro=(new Scanner(System.in)).next().equalsIgnoreCase("y");
            if(!isPro)System.out.println("正常退出!!");
        }
    }
}

求质数

原文:http://www.cnblogs.com/jamsbwo/p/4587063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!