【Language】C#之旅_②_表达式基本应用

来源:互联网 发布:mysql数据库连不上 编辑:程序博客网 时间:2024/06/05 22:41

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

 

                             /*表达式应用*/
 //运算符优先级:
                //元运算符:(x)、x.y、f(x)、a[x]、x++、x--、new、typeof、sizeof、checked、uncheck
                //一元运算符:+、-、~!、++x、--x、(T)x
                //算术运算符:+、-、*、/、%
                //位运算符:<<、>>、&、|、^、~
                //关系运算符:<、>、<=、>=、is、as
                //逻辑运算符:&、|、^
                //条件运算符:&&、||、?
                //赋值运算符:=、+=、-=、*=、/=、<<=、>>=、&=、^=、|=
//除了赋值运算符之外其他运算符都是左结合
          
            //算术运算符
            int a = 10;
            int b = 20;
            int c = 3;
            int x = a + b;
            int y = 1 + 4;
            int z = 1 + a;
            int p = b - a;
            int q = b / y;
            int t = a * c;
            int u = a % c;
            int v = a++;
            int w = ++b;
            Console.WriteLine("a+b="+x);                  
            Console.WriteLine("1+4="+y);
            Console.WriteLine("1+a="+z);
            Console.WriteLine("b-a="+p);
            Console.WriteLine("b/5="+q);
            Console.WriteLine("a*c="+t);
            Console.WriteLine("a%c="+u);
            Console.WriteLine("a++="+v+"此时a="+a);
            Console.WriteLine("++b="+w);

            //关系运算符
            string a1 = "hello";
            string a2 = "hello";
            string a3 = "hey";
            if (a1 == a2)
            {
                Console.WriteLine("相等");
                Console.WriteLine((a1 == a2).ToString());
            }
            else
            {
                Console.WriteLine("不相等");
                Console.WriteLine((a1 == a2).ToString());
            }
            if (a1 == a3)
            {
                Console.WriteLine("相等");
                Console.WriteLine((a1 == a3).ToString());
            }
            else
            {
                Console.WriteLine("不相等");
                Console.WriteLine((a1 == a3).ToString());
            }

            //逻辑运算符
            bool myBool = true;
            bool notTrue = !myBool;
            bool yu = myBool && notTrue;
            bool oth = true;
            if (yu && oth)
            {
                Console.WriteLine("真");
            }
            else
            {
                Console.WriteLine("假");
            }
            int res = 1;
            if (Convert.ToBoolean(res))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

            //位运算符
            int resu = 4;
            Console.WriteLine((resu << 1).ToString());
            Console.WriteLine((resu & res).ToString());
            Console.WriteLine((~resu).ToString());

            //赋值运算符
            int b1, b2, b3,b4;
            b1 = b2 = b3 = b4=1;
            b1 += 1;
            b2 = b2 + 1;
            b3 <<= 1;
            b4 &= 1;
            Console.WriteLine(b1+"  "+b2+"  "+b3+"  "+b4);

            //条件运算符
            bool c1 = true;
            string c2 = c1 ? "true" : "false";
            Console.WriteLine(c2.ToString ());

 
            Console.ReadKey();//等待user输入
        }
    }
}

原创粉丝点击