简体   繁体   English

C#ReadLine问题

[英]C# ReadLine problem

what i want to do is that when people using my program hit enter without anything in there it does not cause an error here is part of the program: 我想做的是,当人们在使用我的程序时,在没有任何输入的情况下进入该程序不会导致错误,这是程序的一部分:

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.
}

the thing is im using integers,i tried to do this 事情是即时通讯使用整数,我试图做到这一点

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.

and different combinations of it >.< and default does not work for that 以及>。<和default的不同组合对此不起作用

i hope some one can help me solve it >.< 我希望有人可以帮助我解决它>。<

Check the Int16.TryParse method. 检查Int16.TryParse方法。

This will allow you to exit from the program or perform another action if the user input is not a number in the range allowed by Int16 (negative 32768 through positive 32767). 如果用户输入的数字不是Int16允许的范围内(负32768到正32767),则这将允许您退出程序或执行其他操作。

Sample code can be found on MSDN entry ( Int16.TryParse Method ). 可以在MSDN条目( Int16.TryParse方法 )上找到示例代码。

First, Set your Console.ReadLine() to a variable. 首先,将Console.ReadLine()设置为变量。 Then check to see if the variable you set is not empty or null. 然后检查设置的变量是否为空或为空。 Also, I'd recommend using the TryParse method of the Int16 class because it returns true or false depending on if the conversion was successful. 另外,我建议使用Int16类的TryParse方法,因为它根据转换是否成功而返回true或false。

Also, you don't need to convert your ReadLine to an integer, because you can switch on Strings also. 另外,您也不需要将ReadLine转换为整数,因为您还可以打开Strings。 Since ReadLine is already a String , no conversion is necessary. 由于ReadLine已经是String ,因此无需进行转换。 However, if you need integers, try this: 但是,如果需要整数,请尝试以下操作:

String lineIn = Console.ReadLine();

if (!String.IsNullOrEmpty(lineIn))
{
    Int16 myNum;
    if (Int16.TryParse(lineIn , out myNum))
    {
            switch(myNum)
            {
                    case 1:
                    ...
                    default:
                    ...
            }
    }
}

我想你想要的是int.Parse(...)

您可能会考虑使用try catch语句进行错误处理...

TryParse: TryParse:

string str;
short val;
while(!short.TryParse(str=Console.ReadLine(), out val))
{
    Console.WriteLine("Cant do that  >.>");
}

For getting an integer i usually use a recursive function like this 为了获得整数,我通常使用像这样的递归函数

private int GetInt()
{
     try
     {
         return int.parse(Console.Readline().Trim());
     } 
      catch (exception e) 
     {
         Console.WriteLine(string.format("{0} Please try again", e.message);
         return GetInt();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM