如何正确使用if语句?

DEV*_*ner 0 c#

由于if声明中的第一行,我无法运行它.我确信有些东西需要转换,但我不确定是什么.

我的代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace xx
{
    class Program
    {
        static void Main(string[] args)
        {

           string userInput;
            Console.Write("what number do you choose: ");
            userInput = Console.ReadLine();

            if (userInput > 100)
                Console.WriteLine("I hate myself");
            else
                Console.WriteLine("I love myself");


        }
    }
}
Run Code Online (Sandbox Code Playgroud)

cor*_*ore 18

userInput是一个字符串,您试图将它与int(100)进行比较.用户输入需要先转换为int.

int i;

// TryParse() returns true/false depending on whether userInput is an int

if (int.TryParse(userInput, out i))
{
    if (i > 100)
    {
        Console.WriteLine("I hate myself");
    }
    else
    {
        Console.WriteLine("I love myself");
    }
}
else
{
    Console.WriteLine("Input was not a valid number.");
}
Run Code Online (Sandbox Code Playgroud)