编写 for 循环找出 1~100 中所有的素数【素数:又被称为质数,能够被1和自身整除,不能被其他数字整除的数字】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public class HelloWorld {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean isSushu = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isSushu = false;
break;
}
}
if (isSushu) {
System.out.print(i + " ");
}
}
}
}
编写 for 循环找出 1~10000 中所有的素数, 要求每 8 个换一行输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public class HelloWorld {
public static void main(String[] args) {
int count = 0;
for (int i = 2; i <= 1000; i++) {
boolean isSushu = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isSushu = false;
break;
}
}
if (isSushu) {
System.out.print(i + " ");
count++;
if (count == 8) {
System.out.println();
count = 0;
}
}
}
}
}