- /*
- * Quiz3
- *
- * 本程序可根据用户的意愿,输出前N位的质数。
- *
- * 2008年12月13日
- *
- *(Cason_xu)
- */
- import java.util.Scanner;
- class Quiz3 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你想要的质数个数");
- int p = sc.nextInt();
- int q = 1;
- for (int n = 2; q <= p;n ++){
- if (isPrime(n)) {
- System.out.print(n + " ");
- q++;
- }
- }
- }
- public static boolean isPrime(int n){
- boolean isPrime = true;
- for (int i = 2; i * i <= n; i++){
- if (0 == (n % i)) {
- isPrime = false;
- break;
- }
- }
- return isPrime;
- }
- }
[Java]根据用户的意愿,输出前N位的质数
最新推荐文章于 2021-09-19 09:27:00 发布