使用循环打印出三角形

时间:2016-07-22 13:00:32

标签: c while-loop variable-initialization

任务是仅使用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;
    }

我不能自己调试它。有人可以为我调试这个吗?

2 个答案:

答案 0 :(得分:5)

你必须将void SetColors(Series s, Color color, double limit) { foreach (DataPoint dp in s.Points) if (dp.YValues[0] < limit) dp.Color = color; } 置于循环内,使其在每个循环中都回零。

k=0

答案 1 :(得分:0)

只需要很少的修正

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

Working Example