加加减减,逻辑运算,if--else结构,求三个数最大,Try--catch的使用

本文通过示例介绍了C#中的算术运算、逻辑判断、if-else结构、条件运算符的使用,并展示了如何进行异常捕获与处理。包括计算表达式、条件判断、最大值查找以及异常输入的处理。最后,演示了根据分数评估成绩等级的多种实现方式。
摘要由CSDN通过智能技术生成

加加减减

            int a = 5;
            int b = a++ + (++a) * 2 + ++a;
            //      5+7*2+8
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.ReadKey();

逻辑运算

       static void Main(string[] args)
        {
            //让用户输入老苏的语文和数学成绩,输出以下判断是否正确,正确输出True,错误输出Falsel、
            //1)老苏的语文和数学成绩都大于90分
            Console.WriteLine("请输入苏小鬼的语文成绩");
            int chinese = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入苏老头的数学成绩");
            int math = Convert.ToInt32(Console.ReadLine());

            //2)语文和数学有一门是大于90分的
            // bool b = chinese > 90 && math > 90;

            bool b = chinese > 90 || math > 90;
            Console.WriteLine(b);
            Console.ReadKey();
        }

if–else结构

  //让用户输入用户名和密码,如果用户名为admin,密码为mypass,则提示登录成功
            Console.WriteLine("请输入用户名!!!");
            string name = Console.ReadLine();

            Console.WriteLine("请输入密码!!!");
            int id = Convert.ToInt32(Console.ReadLine());
            bool m = (name == "admin" && id == 123456);//name== "admin"必须得用双引号 因为是字符串
            if (m)
            {
                Console.WriteLine("登录成功!!!");
            }
            else
            {
                Console.WriteLine("用户名或密码不正确!!!");
            }
            Console.ReadKey();
     static void Main(string[] args)
        {
            //老苏买了一筐鸡蛋,如果鸡蛋少于5个,他就吃掉,否则他就去退货

            Console.WriteLine("请输入老苏鸡蛋个数!!!");
            int egg = Convert.ToInt32(Console.ReadLine());
            if (egg < 5)
            {
                Console.WriteLine("吃掉");
            }
            else
            {
                Console.WriteLine("退货");
            }
            Console.ReadKey();

            //要求用户输入两个数a、b,如果a和b整除或者a加b大于100,则输出a的值,否则输出b的值
            Console.WriteLine("请输入数a");
            int a = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入数b");
            int b = Convert.ToInt32(Console.ReadLine());
            bool m = (a % b == 0) || ((a + b) > 100);
            if (m)
            {
                Console.WriteLine(a);
            }
            else
            {
                Console.WriteLine(b);
            }
            Console.ReadKey();
            //对学员的结业考试成绩评测(考虑用if好还是用if - else好)
            //成绩 >= 90 : A
            //90 > 成绩 >= 80 : B 80 > 成绩 >= 70 : c  70 > 成绩 >= 60 : D
            //成绩 < 60 : E

            Console.WriteLine("请输入学员成绩!!!");
            double gade = Convert.ToDouble(Console.ReadLine());

            #region if-- else if用法
            //if (gade >= 90)
            //{
            //    Console.WriteLine("A");
            //}
            //else if (gade >= 80)
            //{
            //    Console.WriteLine("B");
            //}
            //else if (gade >= 70)
            //{
            //    Console.WriteLine("C");
            //}
            //else if (gade >= 60)
            //{
            //    Console.WriteLine("D");
            //}
            //else
            //{
            //    Console.WriteLine("E");
            //}
            //Console.ReadKey();
            #endregion

            if (gade >= 90)
            {
                Console.WriteLine("A");
            }
            else
            {
                if (gade >= 80)
                {
                    Console.WriteLine("B");
                }
                else
                {
                    if (gade >= 70)
                    {
                        Console.WriteLine("C");
                    }
                    else
                    {
                        if (gade >= 60)
                            Console.WriteLine("D");
                    }
                }
            }
            Console.WriteLine("E");
            Console.ReadKey();
                
        }

求三个数最大

        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个数字");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个数字");
            int b = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第三个数字");
            int c = Convert.ToInt32(Console.ReadLine());

            #region 比较大小第一种
            //if (a > b)
            //{
            //    if (a > c)
            //    {
            //        Console.WriteLine("最大值是{0}", a);
            //    }
            //    else
            //    {
            //        Console.WriteLine("最大值是{0}", c);
            //    }
            //}
            //else
            //{
            //    if (b > c)
            //    {
            //        Console.WriteLine("最大值是{0}", b);
            //    }
            //    else
            //    {
            //        Console.WriteLine("最大值是{0}",c);
            //    }
            //}
            #endregion
            
            //第二种比较三个数的大小
            if (a > b && a > c)
            {
                Console.WriteLine(a);
            }
            else if (b > a && b > c)
            {
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine(c);
            }
            Console.ReadKey(); 

         //第三种使用三元表达式
         int temp=a>b?a:b;
         int max=temp>c?temp:c;
         Console.WriteLine(max);
        }

Try–catch异常捕获

     int number=0;
     bool b=true;
     Console.WriteLine("请输入一个数");
     try
     {
        number=Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("你看我执不执行!!");
     }
     catch
     {
        Console.WriteLine("输入的字符串不能转换成数字,退出程序!!!!");
        b=false;
     }
     if(b)
     {
        Console.WriteLine("输出的数字是{0}",number);
     }
     Console.ReadKey();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值