比较字符串长度和字符串值之间的区别

Soh*_*ail 0 c# string if-statement console-application

namespace ProgrammingTesting
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the input");

            string input1 = Console.ReadLine();
            if (input1 == "4")
            {
                Console.WriteLine("You are a winnere");
                Console.ReadLine();
            }
            else if (input1.Length < 4)
            {
                Console.WriteLine("TOOOOO high");

            }
            else if (input1.Length > 4)
            {
                Console.WriteLine("TOOOO Low");
                                Console.ReadLine();
            }       
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我输入的数字小于4,为什么程序输出"太低".

Llo*_*oyd 5

您没有比较您正在比较输入长度的值.您还需要将字符串的输入转换为整数.例如:

if (int.Parse(input1) < 4) {
    ...
}
Run Code Online (Sandbox Code Playgroud)