[英]C++ for-loops to display a pattern
所以基本上我必须使用for循环来制作这种模式:
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
two blank lines
**********
*********
********
*******
******
*****
****
***
**
*
Two blank lines
*
**
***
****
*****
******
*******
********
*********
**********
我不太确定是否需要将*定义为变量。 我知道我需要使用增量和减量来实现此目的,并且还需要使用理由来正确对齐。 一些反馈和帮助将不胜感激。 谢谢!
这种问题是要教您如何将问题分解为较小的问题,以创建可用于多种模式的代码。
例如,您可以编写一个打印行的函数
void PrintLine(int length)
您可能要指定是要在左侧还是在右侧
void PrintLine(int length, bool left)
然后剩下的就是使循环条件特定于您要显示的形状。
对于第一个形状:
int one=1;
boolean doit=true;
int counter=0;
while(doit)
{
printf("\n");
counter+=one;
for(int i=0;i<counter;i++)
{
printf("*");
}
if(counter>maxi)
{
printf("\n");
for(int i=0;i<counter;i++)printf("*");
one=-one;
}
if(counter==0)doit=false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.