1. 程式人生 > 其它 >加加減減,邏輯運算,if--else結構,求三個數最大,Try--catch的使用

加加減減,邏輯運算,if--else結構,求三個數最大,Try--catch的使用

技術標籤:C#基礎迴圈語句c#

加加減減

            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();