2016-07-22 123 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