目前我正在使用此代码:
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)
但这只是在按下某个键时继续循环。任何解决方案?
我建议使用Console.KeyAvailable:
while (!Console.KeyAvailable) {
Console.WriteLine("Loop");
}
Run Code Online (Sandbox Code Playgroud)