素数又叫质数,质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。
public class primeNumber {
public static void main(String[] args){
int quantity = quantity(101, 200);
System.out.println(quantity);
}
public static int quantity(int start,int end) {
int count = 0;
for (int i = start; i <= end; i++) {
boolean flag = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i + "是素数");
count++;
}
}
return count;
}
}
运行结果: