c - 嵌套循环并创建模式

标签 c loops

我在为我的一项作业创建模式程序时遇到困难。老师要我们编写一个程序,要求用户输入宽度和高度,然后根据输入的内容打印出“*”的图案。我可以打印整个矩形,但她希望它是一个特定的图案。前两行和最后一行应填充星星,中间的行应类似于我在下面提供的示例。必须遵循的规则是宽度必须是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/

相关文章:

jquery 循环创建具有保留值的元素

c - 如何检查嵌套结构的 malloc 结果?在C中

shell - 在 sudo 之后循环查看此处文档中 find 命令的输出

c++ - 中途开始循环

c - 为什么分配 '\0' 会导致段错误?

python - Pandas 返回空的DataFrame,空的列和空的索引

c - C语言中如何按属性对对象进行排序

c - 什么 ABI(如果有的话)限制 [u]intmax_t 的大小?

c - DEFINE_PER_CPU 定义指向结构的指针数组

c++ - 如何在 C/C++ 代码中生成编译器警告 "statement has no effect"