小编Ben*_*ron的帖子

C# - 如何在按下某个键时停止循环?

目前我正在使用此代码:

using System;

namespace Project
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            bool key = false;
            while (key == false)
            {
                Console.WriteLine ("Loop");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个工作正常,但我想让循环在按下一个键时停止。我试过这个:

using System;

namespace Project
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            bool key = false;
            while (key == false)
            {
                Console.WriteLine ("Loop");
                {
                Console.ReadKey (true);
                key = true
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这只是在按下某个键时继续循环。任何解决方案?

c# loops

2
推荐指数
1
解决办法
4549
查看次数

标签 统计

c# ×1

loops ×1