2017-03-05 66 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

相關問題