c#运算符

本文介绍了C#中的基本运算符,包括算数运算如加减乘除,自增自减运算,赋值运算,关系运算符,逻辑运算符,位运算符,移位运算符以及条件运算符。通过示例代码展示了这些运算符的使用方法及其在计算和判断中的作用。
摘要由CSDN通过智能技术生成

算数运算符

运算符

  1. +

  1. -

  1. *

  1. /

  1. %

练习

            int csharp = 90;
            int math = 85;
            int chinese = 100;
            int sum = csharp + math + chinese;
            int average = sum / 3;
            Console.WriteLine("成绩综合为{0},成绩平均值为{1}", sum, average);
            Console.ReadLine();

自增自减运算符

运算符

  1. n++

  1. ++n

  1. n--

  1. --n

练习

            /*int n = 10;
            int result = 10 + n++;
            Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=11;result=20
            Console.ReadLine();*/
            //int n = 10;
            //int result = 10 + ++n;
            //Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=11;result=21;
            //Console.ReadLine();
            //int n = 10;
            //int result = 10 + n--;
            //Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=9;result=20;
            //Console.ReadLine();
            int n = 10;
            int result = 10 + --n;
            Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=9;result=19;
            Console.ReadLine();

赋值运算符

运算符

  1. =

  1. +=

练习

            int a = 10;
            int b=20;
            b += a;//b=20+10
            a = b;//a=b=30
            Console.WriteLine("a的值为{0},b的值为{1}", a, b);
            Console.ReadLine();

关系运算符

运算符

  1. >

  1. <

  1. ==

  1. >=

  1. <=

  1. !=


逻辑运算符

运算符

  1. &&:同真为真

  1. ||:有真为真

  1. !

练习

            Console.WriteLine("打出年份");
            int year=Convert.ToInt32(Console.ReadLine());
            bool flags=(year%400==0||year%4==0||year%100!=0);
            Console.WriteLine(flags);
            Console.ReadLine();

位运算符

运算符

  1. &:位与:a和b对应为同时位1,结果为才为1,否则为0;

  1. |:位或:a和b对应为都是0,结果位才为0,否则为1

  1. ^:位异或:同时为0或1时,结果为0,否则为1;

  1. ~:取反


移位运算符

运算符

  1. <<:左移位运算符

  1. >>:右移位运算符


条件运算符

表达式

a>b?a:b

练习

  Console.WriteLine("打出年龄");
            int year=Convert.ToInt32(Console.ReadLine());
            
            string number = year >= 18 ? "你成年了" : "青少年";
            Console.WriteLine(number);
            Console.ReadLine();

关键

运算符是有优先级的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值