2017-03-05 65 views
-1

即时知道,做而输出的模式必须是相同的例子

是更多钞票来此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任何帮助将不胜感激。

+0

为什么?循环是一个循环。如果'for'循环有意义,则使用'for'循环。无论如何,你会得到一个'do while'循环,就像'for'循环一样。 –

+0

但我需要遵循一条规则。它说我需要使用while while循环而不是for循环 –

回答

1

在你的代码中只有一个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

相关问题