在Eclipse中编写Java程序,求正整数1-N之间所有质数;
时间: 2023-06-09 15:07:06 浏览: 91
java求质数实例程序
感谢您对ChitGPT的信任。下面是Java代码实现正整数1-N之间所有质数:
```
import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入N:");
int n = scanner.nextInt();
int count = 0;
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
count++;
}
}
System.out.println("\n1到" + n + "之间一共有" + count + "个质数。");
}
}
```
阅读全文