C#ReadLine问题

Mak*_*shi 1 c# readline

我想要做的是,当人们使用我的程序命中输入没有任何东西在那里它不会导致错误这是程序的一部分:

Console.WriteLine("4.-Ya no quiero jugar >.<");
int opc = Convert.ToInt16(Console.ReadLine());

switch (opc)
{
    case 1:
        Console.WriteLine("omg");
        break;

    case 2:
        Console.WriteLine("wtf");
        break;

    default:
        Console.WriteLine("Cant do that  >.>");
        Console.ReadKey();
        break;

    etc.
}
Run Code Online (Sandbox Code Playgroud)

事情是使用整数,我试图这样做

string opc1=console.readline();

if (opc =="")
{
    console.writeline("nope,try again");
}
else
{ // Brace was omitted in original - Jon
    int opc = Convert.ToInt16(Console.ReadLine());

    switch (opc)

    blah blah.
Run Code Online (Sandbox Code Playgroud)

它的不同组合>.<和默认值不起作用

我希望有人可以帮我解决它.<

Joã*_*elo 10

检查Int16.TryParse方法.

如果用户输入不是允许范围内的数字Int16(负32768到正32767),这将允许您退出程序或执行其他操作.

示例代码可以在MSDN条目(Int16.TryParse方法)上找到.