程序员开发实例大全宝库

网站首页 > 编程文章 正文

C#编程零基础到入门学习-C#运算符示例补充-源码(4-1)

zazugpt 2024-09-07 01:51:50 编程文章 27 ℃ 0 评论

代码都是源码,一般都是可以复制后直接运行的!

算术运算符

示例及运行结果:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Operator
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 200;
            int b = 300;
            int c;
            float d= 1.0f;
            c = a + b;
           
            Console.WriteLine("第一行 c 的值是 {0}", c);//{}是英文状态
            c = a - b;
            Console.WriteLine("第二行 c 的值是 {0}", c);
            c = a * b;
            Console.WriteLine("第三行 c 的值是 {0}", c);
            c = a / b;
            Console.WriteLine("第四行 c 的值是 {0}", c);
            c = a % b;
            Console.WriteLine("第五行 c 的值是 {0}", c);
            // ++a 先进行自增运算再赋值
            c = ++a;
            Console.WriteLine("第六航c 的值是 {0}", c);
            // --a 先进行自减运算再赋值
            c = --a;
            Console.WriteLine("第七行c 的值是 {0}", c);
            float e = c+d;
            Console.WriteLine("第八行e的值是{0}",e);
            Console.WriteLine("错误的示例运行结果:第9行e的值是{0}", e);//错误的示例
            Console.ReadLine();
        }
    }
}

关系运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Relational
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 30;
            int b = 20;
            if (a == b)
            {
                Console.WriteLine("第一行  a 等于 b");
            }
            else
            {
                Console.WriteLine("第二行 a 不等于 b");
            }
            if (a < b)
            {
                Console.WriteLine("第三行 a 小于 b");
            }
            else
            {
                Console.WriteLine("第四行 a 不小于 b");
            }
            if (a > b)
            {
                Console.WriteLine("第五行 - a 大于 b");
            }
            else
            {
                Console.WriteLine("第六行 - a 不大于 b");
            }
            /* 改变 a 和 b 的值 */
            a = 100;
            b = 400;
            if (a <= b)
            {
                Console.WriteLine("第七行 - a 小于或等于 b");
            }
            if (b >= a)
            {
                Console.WriteLine("第八行 - b 大于或等于 a");
            }
        }
    }
}


逻辑运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logic
{
    internal class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = true;
            if (a && b)
            {
                Console.WriteLine("第一行 - 条件为真");
            }
            if (a || b)
            {
                Console.WriteLine("第二行 - 条件为真");
            }
            /* 改变 a 和 b 的值 */
            a = false;
            b = true;
            if (a && b)
            {
                Console.WriteLine("第三行 - 条件为真");
            }
            else
            {
                Console.WriteLine("第四行 - 条件不为真");
            }
            if (!(a && b))
            {
                Console.WriteLine("第五行 - 条件为真");
            }
            Console.ReadLine();
        }
    }
}



位运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Displacement
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 60;               /* 60 = 0011 1100 */
            int b = 13;               /* 13 = 0000 1101 */
            int c = 0;
            c = a & b;           /* 12 = 0000 1100 */
            Console.WriteLine("第一行 c 的值是 {0}", c);
            c = a | b;           /* 61 = 0011 1101 */
            Console.WriteLine("第二行 c 的值是 {0}", c);
            c = a ^ b;           /* 49 = 0011 0001 */
            Console.WriteLine("第三行 - c 的值是 {0}", c);
            c = ~a;               /*-61 = 1100 0011 */
            Console.WriteLine("第四行 - c 的值是 {0}", c);
            c = a << 2;     /* 240 = 1111 0000 */
            Console.WriteLine("第五行 - c 的值是 {0}", c);
            c = a >> 2;     /* 15 = 0000 1111 */
            Console.WriteLine("第六行 - c 的值是 {0}", c);
            Console.ReadLine();
        }
    }
}



赋值运算符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assigning
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 21;
            int c;
            c = a;
            Console.WriteLine("第一行 - =  c 的值 = {0}", c);
            c += a;
            Console.WriteLine("第二行 - += c 的值 = {0}", c);
            c -= a;
            Console.WriteLine("第三行 - -=  c 的值 = {0}", c);
            c *= a;
            Console.WriteLine("第四行 - *=  c 的值 = {0}", c);
            c /= a;
            Console.WriteLine("第五行 - /=  c 的值 = {0}", c);
            c = 200;
            c %= a;
            Console.WriteLine("第六行 - %=  c 的值 = {0}", c);
            c <<= 2;
            Console.WriteLine("第七行 - <<=  c 的值 = {0}", c);
            c >>= 2;
            Console.WriteLine("第八行 - >>=  c 的值 = {0}", c);
            c &= 2;
            Console.WriteLine("第九行 - &=  c 的值 = {0}", c);
            c ^= 2;
            Console.WriteLine("第十行 - ^=  c 的值 = {0}", c);
            c |= 2;
            Console.WriteLine("第十一 - |=  c 的值 = {0}", c);
            Console.ReadLine();
        }
    }
}


运算符优先级

在进行混合运算的时候的会涉及到运算符的谁先谁后(优先级)问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hybrid
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 4;
            int c = 6;
            int d = 24;
            int e;
            e = (a + b) * c / d; 
            Console.WriteLine("(a + b) * c / d 的值是 {0}", e);
            e = ((a + b) * c) / d; 
            Console.WriteLine("((a + b) * c) / d 的值是 {0}", e);
            e = (a + b) * (c / d); 
            Console.WriteLine("(a + b) * (c / d) 的值是 {0}", e);
            e = a + (b * c) / d; 
            Console.WriteLine("a + (b * c) / d 的值是 {0}", e);
            e = a + b * c / d;
            Console.WriteLine("a + b * c / d的值是 {0}", e);
            e = a + b + c + d;
            Console.WriteLine("a + b + c + d的值是 {0}", e);
            Console.ReadLine();
        }
    }
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表