我在为我的一项作业创建模式程序时遇到困难。老师要我们编写一个程序,要求用户输入宽度和高度,然后根据输入的内容打印出“*”的图案。我可以打印整个矩形,但她希望它是一个特定的图案。前两行和最后一行应填充星星,中间的行应类似于我在下面提供的示例。必须遵循的规则是宽度必须是6的倍数并且高度必须更大
模式示例:
111111111111111111111111111111111111
111111111111111111111111111111111111
111 111 111 111 111 111
111 111 111 111 111 111
111 111 111 111 111 111
111111111111111111111111111111111111
111111111111111111111111111111111111
我的代码:
int width, height, i, j, space;
//Program asks user to enter width of their fabric
printf("What is the width of your fabric?\n");
scanf("%d", &width);
while(width % 6 !=0){
printf("Please enter a width that is a multiple of 6\n");
scanf("%d", &width);
}
//Program asks user to enter height of their fabric
printf("What is the height of your fabric?\n");
scanf("%d", &height);
while(height <= 6 || height % 2 == 0){
printf("Please enter an odd height which is at least 7\n");
scanf("%d", &height);
}
for(i=1; i<=height; i++)
{
for(j=1; j<=width; j++)
{
printf("*");
}
printf("\n");
}
最佳答案
您需要在循环内使用 if 语句并根据打印的行(变量 i)调整宽度。 您可能希望将 i 重命名为 currentRow,将 j 重命名为 currentColumn。
关于c - 嵌套循环并创建模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946759/