编程3 使用while,完成以下图形的输出 * * * * * * * * * * * * * * * * * * * * * * * * *
时间: 2023-09-06 09:11:19 浏览: 368
下面是使用 while 循环完成该图形输出的代码:
```python
n = 5
i = 1
while i <= n:
j = 1
while j <= i:
print("*", end=" ")
j += 1
print()
i += 1
i = n - 1
while i >= 1:
j = 1
while j <= i:
print("*", end=" ")
j += 1
print()
i -= 1
```
输出结果为:
```
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
```
希望对你有帮助!如果还有其他问题,请随时提问。
相关问题
用c语言编程输出以下图形。 * * * * * * * * *
这里提供两种方法来用C语言编程输出该图形:
方法一:嵌套循环-for循环嵌套
```
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
for (i = 1; i <= 3; i++) //外层循环控制行数
{
for (j = 1; j <= 2 * i - 1; j++) //内层循环控制每行输出的*数
{
printf("* ");
}
printf("\n");
}
system("pause");
return 0;
}
```
方法二:while嵌套循环-while循环里面再嵌套一个while循环
```
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
int main()
{
int a, b; //定义两个变量-a用来控制内循环;b用来控制外循环
char x = '*';
a = 1;
while (a <= 3) //外循环-大环境-while控制行数
{
b = 1;
while (b <= 2 * a - 1) //内循环-while控制输出*的个数
{
printf("%c ", x);
b++;
}
printf("\n");
a++;
}
return 0;
}
```
c编程输出如下图形。 ** ** * * * * * ***
这个图形可以使用以下两种方法来输出:
1.方法一:使用多个printf语句输出
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("** **\n");
printf("* *\n");
printf("* * \n");
printf("* \n");
printf("***\n");
system("pause");
return 0;
}
2.方法二:使用while嵌套循环输出
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
int main() {
int a ,b; //定义两个变量-a用来控制内循环;b用来控制外循环
char x = '*';
a = 1;
while (a <= 5) //外循环-大环境-while控制行数
{
b = 1;
while (b <= 5) //内循环(五脏六腑)-while控制输出*的个数-每五个为一行
{
if ((a == 1 && (b == 1 || b == 2 || b == 6 || b == 7)) || (a == 2 && (b == 1 || b == 6 || b == 7)) || (a == 3 && (b == 1 || b == 5 || b == 6 || b == 7)) || (a == 4 && (b == 1 || b == 5 || b == 7)) || (a == 5 && (b == 1 || b == 2 || b == 3 || b == 4 || b == 5 || b == 7)))
{
printf("%c", x);
}
else
{
printf(" ");
}
b++;
}
printf("\n");
a++;
}
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)