Casting Y or N to bool C#

前端 未结 10 2449
故里飘歌
故里飘歌 2021-02-07 05:46

Just for neatness sake I was wondering, whether it\'s possible to cast Y or N to a bool? Something like this;

bool theanswer = Convert.ToBoolean(input);
         


        
10条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 06:27

    class Program
    {
        void StringInput(string str)
        {
            string[] st1 = str.Split(' ');
    
            if (st1 != null)
            {
                string a = str.Substring(0, 1);
                string b=str.Substring(str.Length-1,1);
    
                 if(
                    a=="^" && b=="^" 
                    || a=="{" && b=="}" 
                    || a=="[" && b=="]"
                    ||a=="<" && b==">" 
                    ||a=="(" && b==")"
                    )
    
                {
                    Console.Write("ok Formate correct");
                }
                else
                {
                    Console.Write("Sorry incorrect formate...");
                }
            }
        }
        static void Main(string[] args)
        {
            ubaid: ;
            Program one = new Program();
            Console.Write("For exit Press N ");
            Console.Write("\n");
            Console.Write("Enter your value...=");
            string ub = Console.ReadLine();
    
            if (ub == "Y" || ub=="y" || ub=="N" || ub=="n" )
            {
                Console.Write("Are your want to Exit Y/N: ");
                string ui = Console.ReadLine();
                if (ui == "Y" || ui=="y")
                {
                    return;
                }
                else
                {
                    goto ubaid;
                }
    
            }
            one.StringInput(ub);           
            Console.ReadLine();
            goto ubaid;
        }
    }
    

提交回复
热议问题