执行输出模式必须与示例相同

do while pattern of output must be the same as example

本文关键字:输出模式 执行      更新时间:2023-10-16

我想知道,

for loop内部将此for loop转换为do while loop

是否也可以选择

这是代码:

int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
    for(j=1; j<=i; ++j)
    {
        printf("* ");
    }
    printf("n");
}

这是我尝试的:

    int i, j, rows;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    i=1;
    j=1;
do {
    printf("* ");
    i<=rows;
    ++i;
    ++j;
} while (j<=i);
    printf("n");
    getch();
    return 0;
}

尽我所能,但没有运气。顺便说一句,我试图使用do while loop进行星形模式。

在您的代码中您只有一个do while循环,您应该使用两个嵌套的do while循环以实现您的目标:

    i=1;
    j=1;
do {
   do {   
    printf("* ");
    ++j;     
   } while (j<=i);
    printf("n");
    i++;
    j=1;
} while(i <= rows);

测试程序:https://ideone.com/qhn3l4