- import java.util.Scanner;
- class Demo2_1 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你要检测素数的开始值:");
- int start = sc.nextInt();
- System.out.println("请输入你要检测素数的结束值:");
- int end = sc.nextInt();
- int count = 0;
- for (int i=start; i<end; ++i){
- if (isSuShu(i)) {
- System.out.println(i);
- count++;
- }
- }
- System.out.println("从" + start + "到" + end + "共有" + count +"个素数!");
- }
- public static boolean isSuShu(int n) {
- boolean flag = false;
- if (n == 1) {
- flag = false;
- }else {
- for (int i=2; i<n; ++i){
- if (n % i == 0) {
- flag = false;
- break;
- }else {
- flag = true;
- }
- }
- }
- return flag;
- }
- }
复制代码
刚出炉的。分享一下:
|
|