package itcast;
public class Fifth {
/**
* 1--100的质数
*/
public static void main(String[] args) {
demo1();
int count = 0;
System.out.println("所有的质数:");
System.out.println(2);
System.out.println(3);
for (int i = 1; i <= 100; i++) {
boolean b = false;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
b = false;
break;
}else {
b = true;
}
}
if (b == true) {
System.out.println(i);
count++;
}
}
System.out.println("1-100间的质数个数为" + (count+2));
}
private static void demo1() {
int count = 0;
for (int i = 2; i <= 100; i++) {
boolean b = false;
for (int j = 1; j < 100; j++) {
if(i % j == 0) {
count++;
}
}
if(count == 2) {
System.out.println(i);
}
}
}
}
|
|