求质数

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("正常退出!!");
        }
    }
}
posted @ 2015-06-18 21:13  jamsbwo  阅读(196)  评论(0编辑  收藏  举报
©2014 to