2016-07-22 207 views
0

任務是僅使用while循環打印以下形狀。使用循環打印出三角形

* 
** 
*** 
**** 
***** 
****** 
******* 
******** 
********* 

下面的代碼是什麼我已經試過了,但它不工作,遺憾的是:

#include "stdafx.h"//Visual Studio 2015 
#include <stdio.h> 
#include <stdlib.h>// using for command system("pause") ; 
#include <math.h> 


    int main() 
    { 
     int i=0, k=0; 
     while (i < 10) 
     { 
      while (k <= i) 
      { 
       printf("*"); 
       k++; 
      } 
      printf("\n"); 
      i++; 
     } 
     system("pause"); 
     return 0; 
    } 

我不能由我自己調試。任何人都可以爲我調試這個嗎?

+3

移動聲明和初始化到零k'的'* *內的'而(I <10)'環。 – WhozCraig

回答

5

您必須在循環內放置k=0,以使其在每個循環中都回到零。

int main() { 
     int i=0, k=0; 
     while (i < 10) 
     { 
      k=0; //<-- HERE 
      while (k <= i) 
      { 
       printf("*"); 
       k++; 
      } 
      printf("\n"); 
      i++; 
     } 
     system("pause"); 
     return 0; 
    } 
0

它只需要很少的校正

int i=0; 
    while (i < 10) 
    { 
    int k=0; 
     while (k <= i) 
     { 
      printf("*"); 
      k++; 
     } 
     printf("\n"); 
     i++; 
    } 

Working Example

+0

修復程序的描述對於操作會更有幫助 – Javant