目前我正在使用这段代码:
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 https://msdn.microsoft.com/en-us/library/system.console.keyavailable(v=vs.110).aspx:
while (!Console.KeyAvailable) {
Console.WriteLine("Loop");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)