问答详情
源自:4-9 C#循环结构之嵌套循环

关于for循环和while循环的转换

static void Main(string[] args)
        {
            for (int y = 1; y <= 7; y++)
            {
                for (int x = 1; x <= y; x++)
                {
                    Console.Write(x);
                }
                Console.WriteLine();//换行
            }
        }

第二个for循环能改成while循环吗?本人愚拙,望大神赐教。

提问者:故月晚风 2018-04-25 00:38

个回答

  • 慕移动526555
    2018-05-21 21:41:15

    for (int y = 1; y <= 7; y++)

                {

                    for (int x = 1; x <= 7; x++)

                    {

                        if(x<=y)

                        Console.Write(x);

                    }

                    Console.WriteLine();//换行

                }


  • 慕丝3378769
    2018-04-25 01:28:39

    可以改但是得先声明变量x

    static void Main(string[] args)
            {
                int x = 1

                for (int y = 1; y <= 7; y++)
                {
                    while(x <= y)
                    {
                        Console.Write(x);

                        x++:
                    }
                    Console.WriteLine();//换行
                }
            }