标签:
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