C#-如何在按下键时停止循环? [英] C# - How to Stop a Loop When a Key is Pressed?

查看:90
本文介绍了C#-如何在按下键时停止循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我正在使用以下代码:

Currently I am using this code:

using System;

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

哪个可以正常工作,但是我想在按下某个键时使循环停止.我试过了:

Which works fine, but I wanted to make the loop stop when a key is pressed. I tried this:

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
                }
            }
        }
    }
}

但这只是在按下某个键时继续循环.有解决方案吗?

But that just continues the loop when a key is pressed. Any solutions?

推荐答案

我建议使用

这篇关于C#-如何在按下键时停止循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆