目前我正在使用此代码:
using System;
namespace Project
{
class MainClass
{
public static void Main (string[] args)
{
bool key = false;
while (key == false)
{
Console.WriteLine ("Loop");
}
}
}
}
这工作正常,但我想让循环在按下按键时停止。我试过这个:
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
}
}
}
}
}
但是当按下一个键时,这只会继续循环。有什么解决办法吗?
最佳答案
我建议使用Console.KeyAvailable :
while (!Console.KeyAvailable) {
Console.WriteLine("Loop");
}
关于C# - 如何在按下按键时停止循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542248/