java输出30以内所有自然数(不包含30),分5行输出,一行6个数,格式如下: 0 1 2 3 4 5 6 7 8 9 10 11
时间: 2024-05-07 14:19:49 浏览: 13
以下是 Java 代码实现:
```java
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 30; i++) {
if (count == 6) {
System.out.println();
count = 0;
}
System.out.print(i + "\t");
count++;
}
}
}
```
输出如下:
```
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
```
相关问题
输出30以内所有自然数(不包含30),分5行输出,一行6个数,格式如下: 0 1 2 3 4 5 6 7 8 9 10 11
Sure! 下面是代码实现:
```python
for i in range(0, 30):
if i % 6 == 0:
print("\n", end="")
if i != 29:
print("{0:2d}\t".format(i), end="")
else:
print("{0:2d}".format(i), end="")
```
输出结果如下:
```
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
```
希望能够帮到你!
Java 输出2-100的所有素数(只能被1 和自己整除的自然数),每行输出5个
素数的算法是一个经典的问题,可以使用以下代码来解决:
public class PrimeNumbers {
public static void main(String[] args) {
int count = 0;
int num = 2;
while (num <= 100) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
count++;
if (count % 5 == 0) {
System.out.println();
}
}
num++;
}
}
}
输出结果如下:
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)