今天做了个程序题中class Student
{  ......
   public bool sex;
   if(sex==true)
   {} 
   else 
   {}
   ........
}
如何在Main()函数中对bool值进行赋值()
我想从控制台中输入true 或者 flase来显示if语句中的信息

解决方案 »

  1.   

    输入的话是字符了,string
    可以这样
    if (sex.ToLower()=="true")
    {
    }
    else
    {
    }
      

  2.   

    用sex.ToString().equals(yourinput);就行了yourinput是你从控制台读入的True或False
      

  3.   

    bool input = bool.Parse(System.Console.ReadLine());
      

  4.   

    public bool sex;
       if(sex==true)
       {} 
       else 
       {}
    ------------
    这样些
    if(sex)
    {}
    else
    {}
      

  5.   

    非常同意cyy1981(McRain),参考《Effective C++》,平时养成好的编程习惯!!!
      

  6.   

    bool input = bool.Parse(System.Console.ReadLine());看上去正解,支持!
      

  7.   

    string sex=Console.ReadLine().ToLower().Trim();
    if(sex=="true")
    {}
    else
    {}
    这样多直接
      

  8.   

    !!!!~~~~~~~~>>>>>>>>>
      

  9.   

    最好这么写,这样可以处理用户的错误输入:string input = Console.ReadLine();
    bool sex;
    if (!bool.TryParse(input, out sex))
    {
        Console.WriteLine("Can not parse \"" + input + "\" to bool!");
    }
    if (sex)
    {
        // ...
    }
    else
    {
        // ...
    }