2017-05-09 68 views
-1

嘿,我正在研究CS50更舒適的問題,我無法弄清楚如何在同一行上打印第二個馬里奧金字塔。在我的代碼中它已經打印出來,但它不在同一行。打印出馬里奧兩個半金字塔CS50

如果你指導我或告訴我如何去做,那並不重要。我正在使用CS50作爲練習,我沒有打開任何東西,所以這不會作弊。

#include <stdio.h> 
#include <ctype.h> 

int main(void) 
{ 
    int height = 0; 

    // left pyramid variables 
    int i = 0; 
    int j = 0; 
    int k = 0; 

    // variable for gap 
    int g = 0; 

    // right pyramid variables 
    int l = 0; 
    int m = 0; 
    int n = 0; 


    //do - while loop -- works 

    do 
    { 
     printf("Height: "); 
     scanf("%d", &height); 
    } 

    while (height < 0 || height > 23); 

    // Print pyramids 

     // print spaces for left pyramid (less spaces needed with time) ✓ 
     // print hashes for left pyramid ✓ 
     // print gap (2) 
     // print hashes for right pyramid 
     // print new line - for next row 

    // Left Pyramid 

    // Rows -- determines the height 
    for (i = 0; i < height; i++) 
    { 
     // Cols -- in this one we are doing the spaces 

     // the -i makes it left aligned -- to make it right aligned remove the "-1" 
     for (j = 0; j < height-i; j++) 
     { 
      // Printing Spaces 
      printf(" "); 
     } 
     // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one 
     // whenever the height increases, so that's why we add + 1 to it 
     // if I don't add 1 to it what it does is that prints a new line, and then it prints 
     // 4 things instead of 5 for example. 
     for (k = 0; k < i + 1; k++) 
     { 
      printf("#"); 
     } 

     // Print new line 
     printf("\n"); 
    } 

    // Gap -- fix gap, the rest works how it should -- I think I need to make everything 
    // inside one loop 

    // for (g = 0; g < height; g++) 
    // { 
    //  printf(" "); 
    // } 

    // Right Pyramid 

     // Rows -- determines the height 
    for (l = 0; l < height; l++) 
    { 

     // Cols -- in this one we are doing the spaces 

     // right aligned 
     for (m = 0; m < height; m++) 
     { 
      // Printing Spaces 
      printf(" "); 
     } 
     // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one 
     // whenever the height increases, so that's why we add + 1 to it 
     // if I don't add 1 to it what it does is that prints a new line, and then it prints 
     // 4 things instead of 5 for example. 
     for (n = 0; n < l + 1; n++) 
     { 
      printf("#"); 
     } 

     // Print new line 
     printf("\n"); 
    } 


    return 0; 
} 
+0

名變量爲正確的:不是,指出「//爲缺口變量」 ..致電變量「空隙」評論!或者更具描述性的東西 –

+0

我在代碼評論中溺水! – paddy

+0

由於換行符結束一行,並且沒有選項返回到上一行(當然,不使用基本操作),您只需要同時向左和向右打印出來。 – Evert

回答

0

爲什麼不這樣做在第一循環:

for (k = 0; k < i + 1; k++) 
{ 
    printf("#"); 
} 

/* this is new */ 
/* Draw the gap */ 
for (k = 0; k < gap; k++) { 
    printf(" "); 
} 
/* Draw the left part*/ 
for (k = 0; k < i + 1; k++) 
{ 
    printf("#"); 
} 
+0

我會嘗試。我還必須包括每個#和左右金字塔之間的兩個空格之間的空格。感謝您的輸入。我很感激 –

+0

謝謝你。有效。唯一不起作用的是兩個金字塔之間的兩個空隙。但其餘的作品:D 你能幫助我解決兩個空間的差距嗎? –

+0

NVM我想出了差距 我做了以下操作: for(k = 0; k <1; k ++){ printf(「」); } –