刷题的时候总会遇到那种多个测例,需要循环输入,现总结几种常用并简洁优(cu)雅(bao)的方法。
1.适合空白字符输入后进行操作会报错的,比如有string转int操作的
namespace Test
{
class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
var next = Console.ReadLine();
//other process code
}
catch
{
break;
}
}
}
}
}
2.适合确定输入空白字符后进行操作不会报错的
namespace Test
{
class Program
{
static void Main(string[] args)
{
var next = Console.ReadLine();
while (!string.IsNullOrWhiteSpace(next))
{
//process code
next = Console.ReadLine();
}
}
}
}