简体   繁体   English

如何检查变量是否等于字符串或整数?

[英]How do I check if a variable is equal to a string or an int?

For example, lets say I have and int called input. 例如,假设我有int称为input。 And then I get input for it. 然后我得到输入。 Like this: 像这样:

input = Console.ReadLine();

But when the user gets prompted, input is a string, and then it gets an error and crashes the program. 但是,当提示用户输入时,输入是一个字符串,然后出现错误并使程序崩溃。 How do I check if a string was entered for input? 如何检查是否输入了字符串?

Is there an option for it like if (input.equals(string)) or something like that? 是否有类似if (input.equals(string))之类的选项?

EDIT: I tried doing the TryParse but I think I'm doing it wrong. 编辑:我试图做TryParse,但我认为我做错了。 I posted the code below 我在下面发布了代码

    string numberOne;
    string numberTwo;
    double answer;
    string operand;

    Console.WriteLine("Enter the first number");
    numberOne = Console.ReadLine();
    if (double.TryParse(numberOne, out double value))
    {
        Convert.ToInt32(numberOne);
    }
    else
    {
        Console.WriteLine("Non integer entered. Please enter a valid number.");
    }

    Console.WriteLine("Enter the operator");
    operand = Console.ReadLine();

    Console.WriteLine("Enter the second number");
    numberTwo = Console.ReadLine();
    if (double.TryParse(numberTwo, out double value2))
    {
        Convert.ToInt32(numberTwo);
    }
    else
    {
        Console.WriteLine("Non integer entered. Please enter a valid number.");
    }




    switch(operand)
    {
        case "+":
            answer = numberOne + numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "-":
            answer = numberOne - numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "*":
            answer = numberOne * numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        case "/":
            answer = numberOne / numberTwo;
            Console.WriteLine("The answer is " + answer);
            break;

        default:
            Console.WriteLine("Please enter a valid operator such as +, -, *, or /");
            break;



    }

Console.Readline() always returns a string, however you can do this to check if an integer is inputed and continue asking for correct type of input until user delivers it. Console.Readline()始终返回一个字符串,但是您可以执行此操作以检查是否输入了整数,并继续要求输入正确的类型,直到用户交付为止。

string input = Console.ReadLine();
int parsed;
while (!int.TryParse(input, out parsed))
{
     Console.WriteLine("Please enter a number!");
     input = Console.ReadLine();
}

//do something with parsed

Console.ReadLine always returns a string . Console.ReadLine始终返回一个string It's doesn't return differently typed things based the content read from console. 它不会根据从控制台读取的内容返回不同类型的内容。 It's your job to decide if the value entered matches your requirements (in this case it should be something that can be parsed to an int). 确定输入的值是否符合您的要求是您的工作(在这种情况下,应该是可以解析为int的值)。

You can use int.TryParse to try parsing a string as int without it throwing an exception when parsing fails. 您可以使用int.TryParse尝试将字符串解析为int而不会在解析失败时引发异常。

string input = Console.ReadLine();
if(int.TryParse(input, out int value))
{
    // value contains an int parsed out of input
}
else
{
    // parsing fails, you can do something about that, e.g. ask user for different input
}

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

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