打印以下图案(用循环实现)。 * *** ***** ******* ***** *** *
时间: 2023-11-25 18:05:15 浏览: 76
是Java代码实现:
```java
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 3; i >= 1; i--) {
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
```
输出结果为:
```
*
***
*****
*******
*****
***
*
```
以下是C++代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int num;
while (cin >> num) {
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
for (int i = num - 1; i >= 1; i--) {
for (int j = 1; j <= num - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
}
return 0;
}
```
输出结果为:
```
*
***
*****
*******
*********
*******
*****
***
*
```
阅读全文