C#編程基礎第三章:C#運算符和選擇語句


一、說在最前面的話

1、老師對大家的要求

  • 課堂紀律
  • 上課風格
  • 課后作業
  • 學習小組(班長,學委)
  • 機房管理
  • 違規管理

2、大家對老師的要求

3、軟件工程專業的前景

https://www.lagou.com/

https://www.51job.com/

http://www.gaosan.com/gaokao/76642.html

二、內部測試

 1、請使用C#編程實現在控制台輸入你的姓名,輸出問好信息,如:

請輸入:張三

Hello,張三!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test01
{
    class Program
    {
        //ctrl+f5
        static void Main(string[] args)
        {
            Console.Write("請輸入:");
            //定義變量,賦值為張三
            //從鍵盤輸入,賦值給變量name
            String name =Console.ReadLine();
            Console.WriteLine("Hello "+name+"");
        }
    }
}

結果:

            String name = "";
            do
            {
                Console.Write("請輸入:");
                //定義變量,賦值為張三
                //從鍵盤輸入,賦值給變量name
                name = Console.ReadLine();
                Console.WriteLine("Hello " + name + "");
            } while (name != "");
View Code

2、請使用C#實現計算圓的面積,輸入半徑,顯示圓的面積,如:

請輸入圓的半徑r:5

圓的面積是:78.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test02
{
    class Program
    {
        static void Main(string[] args)
        {
            //ctrl+k ctrl+c
            //2、請使用C#實現計算圓的面積,輸入半徑,顯示圓的面積,如:
            //    請輸入圓的半徑r:5
            //    圓的面積是:78.5
            Console.Write("請輸入圓的半徑r:");
            //從鍵盤輸入字符類型的半徑,轉換成double類型
            double r =Convert.ToDouble(Console.ReadLine());

            //計算面積
            double area = 3.14 * r * r;

            //輸出
            Console.WriteLine("圓的面積是:{0:N2}",area);
        }
    }
}

輸出:

3、使用C#編程實現簡單的四則運算,輸入兩個數,輸出這兩個數的加減乘除四則運算,如:

請輸入x:8.5
請輸入y:5.3
8.5+5.3=13.8 8.5-5.3=3.2 8.5*5.3=45.05 8.5/5.3=1.6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test03
{
    class Program
    {
        static void Main(string[] args)
        {
            //3、使用C#編程實現簡單的四則運算,輸入兩個數,輸出這兩個數的加減乘除四則運算,如:

            //    復制代碼
            //    請輸入x:8.5
            //    請輸入y:5.3

            //提示
            Console.Write("請輸入x:");
            //輸入字符,轉換成decimal類型,賦值給變量x
            decimal x =Convert.ToDecimal(Console.ReadLine());

            Console.Write("請輸入y:");
            decimal y = Convert.ToDecimal(Console.ReadLine());

            Console.WriteLine(x+" + "+y+"="+(x+y));
            Console.WriteLine("{0} - {1}={2}",x,y,x-y);
            Console.WriteLine("{0} x {1}={2}", x, y, x * y);
            Console.WriteLine("{0} / {1}={2}", x, y, x / y);
        }
    }
}

輸出:

 

 拓展:

1、當y為0時,提示用戶輸入錯誤,if判斷

2、一次運行可以反復計算,do---while循環

三、運算符

 3.1、賦值運算符

C#中常見的賦值運算符:

“=”號是基本賦值運算符,其它的+=、-=、*=、/=、%=稱為復合賦值運算符。這些運算符的左邊一定是變量,右邊可以是表達式。復合賦值的運算規則簡單說就是在左邊變量原來值的基礎上跟右邊的表達式作相應運算,再把計算結果賦值給左邊的變量。它們的使用請看如下代碼:

int x=40; //將40賦給x變量
x+=30; //相當於x=x+30,此時得到x=70
x-=20; //相當於x=x-20,此時得到x=50
x*=2; //相當於x=x*2,此時得到x=100
x/=4; //相當於x=x/4,此時得到x=25
x%=3; //相當於x=x%3,此時得到x=1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P01
{
    class Program
    {
        static void Main(string[] args)
        {
            int x=5,y=6;

            //x = x + y;

            //x += y;  //x=x+y;

            //y += y;  //y=y+y;

            //x += x;  //x=x+x;

            //y += (y - x);  //y=6+1

            //y -= x + 3 * y; //y=y-(x+3*y)

            //求余 x%2=0 x%2=1
            //y %=x;  //y=y%x;   y=6%5;

            x *= y % 2 - x + y;  //x=x*(y%2-x+y)  //x=5*(0-5+6)

            Console.WriteLine(x);
        }
    }
} 

3.2、算術運算符

 C#中常見的算術運算符,如下表所示:

由算術運算符組成的表達式稱為算術表達式。對這些運算符的說明如下:
 +號除了表示加法運算以外,還可以表示字符串的連接或表示正數的正號;
 -號表示減法運算外,還可以表示負數的負號;
 /號表示除法運算;
 %號表示求模運算,在商為整數的情況下,得到的余數作為結果;
 ++和--都有前綴和后綴兩種形式,++表示自身的值增加 1,--表示自身的值減少 1。前綴和后
綴的差別主要在於優先級的不同,前綴優先級高,后綴優先級低。

課堂作業:

(1)int x=30,y=0; x+=10; y-=x; 請問 y 等於多少?

(2)int x=40; int y=--x + 10; 請問 y 等於多少?

3.3、關系運算符

C#中提供了多種關系運算符,如下表所示:

 

 由關系運算符組成的表達式稱為關系表達式。從上表中可以看出,關系表達式的結果只能是 true

或者 false,也就是布爾類型的結果。

注意:在程序中判斷是否相等,要用兩個等號。

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

namespace P03
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 9, y = 2;

            Console.WriteLine(x==y);  //false 假
            Console.WriteLine(x != y);  //true  真
            Console.WriteLine(x > y);  //true 真
            Console.WriteLine(x >= y);  //true 真
            Console.WriteLine(x < y);  //false 假
            Console.WriteLine(x <= y);   //false 假
        }
    }
}

3.4、邏輯運算符

C#中的邏輯運算符有 3 種,如下表所示:

 由邏輯運算符組成的表達式稱為邏輯表達式,跟關系表達式一樣,它的結果也只能是 true 或

false。關系表達式和邏輯表達式也可稱為布爾表達式。布爾表達式通常出現在選擇、循環等語句中表

示判斷條件。

int x=5,y=10,z=15;
char c='x';
Console.WriteLine(x==y); //false
Console.WriteLine(x<=y); //true
Console.WriteLine(x<y&&y<z); //true
Console.WriteLine(x>y||y<z); //true
Console.WriteLine(!(x<y)); //false
Console.WriteLine(c>='a'&&c<='z'); //true

課堂作業:使用關系或邏輯運算符分別寫出如下問題的表達式。

(1)一個整數 a 是偶數;

(2)一個學生的成績 b 在 0 到 100 之間;

(3)一個數 c 小於 1 或者大於 12

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

namespace P04
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 9, y = 2,z=1;

            //&& 與運算,同時為真時結果為真,其余情況都為假
            Console.WriteLine(x>y&&x>z);  //真true 假false
            Console.WriteLine(x<y&&x>z);  //真true 假false

            //|| 或運算,同時為假時,結果為假,其余情況都為真
            Console.WriteLine(x > y || x > z);  //真true 假false
            Console.WriteLine(x < y || x > z);  //真true 假false
            Console.WriteLine(x <= y || x <= z);  //真true 假false

            //! 非運算,真變假,假變真
            Console.WriteLine(!(x>y));
            Console.WriteLine(!(x<z));
        }
    }
}

3.5、運算符的優先級

當計算有兩個或兩個以上運算符的表達式時,就需要考慮運算符的優先級。下表給出了常見運算

符的優先級關系。

 在實際的應用中,擅於給表達式中某些優先運算部分加(),是一個很好的做法。這樣就能更清楚

知道表達式的計算順序了。

課堂作業:計算出下列表達式的結果。

(1) 30 / (7 + 2) < 4 || 6 % 5 == 1;

(2) 30 > 6 * 2 && 40 - 4 * 3 / 6 < 40;

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

namespace p06
{
    class Program
    {
        static void Main(string[] args)
        {
            //int n =Convert.ToInt32(Console.ReadLine());
            for (int n = 0; n <= 10000; n++) 
            {
                if (n % 2 == 0)
                {
                    Console.Write("{0}偶數",n);
                }
                else
                {
                    Console.Write("{0}奇數", n);
                }
            }
            Console.WriteLine("over");
        }
    }
}
View Code

四、選擇語句

在生活中,經常要作出選擇,比如,當一個人走到岔路口時,擺在面前有兩條路,那么應該選擇
哪條路走呢?這種情況也經常出現在程序中,選擇語句也就派上了用場。選擇語句也可稱為分支流程
結構,主要包括兩種語句:if 和 switch

4.1、 if 語句

if 語句,主要有 3 種形式。

(1) 形式一 ,單分支流程語句,語法結構如下:

if(布爾表達式)
{
語句塊;
}

例 3.1 輸入成績,判斷是否及格。

(2) 形式二 ,雙分支流程語句,語法結構如下:

if(布爾表達式)
{
語句塊1; 
}
else
{
語句塊2;
}

此種語法的含義是,當布爾表達式的值為 true 時,執行語句塊 1;否則,執行語句塊 2。

例 3.2 在上面的例 3.1 中使用了單分支形式的 if 語句,當及格時顯示恭喜您,當不及格時則程
序什么都不做,如果我們希望不及格也要有對應的提示信息則需要使用雙分支的 if 語句,代碼如下:

三目運算符

對於雙分支的 if…else 語句,在 C#中,有一個可以達到類似效果的運算符:三目運算符(?:)。
在某些情況下使用,它可以簡化我們的程序代碼。三目運算符需要三個操作數,使用形式如下:

布爾表達式?表達式1:表達式2

它的求值規則為:計算布爾表達式的值,如果為 true,則返回表達式 1 的結果;如果為 false,
則返回表達式 2 的結果。

 (3) 形式三 ,多分支流程語句,語法結構如下:

if(布爾表達式1)
{
語句塊1; 
}
else if(布爾表達式2)
{
語句塊2;
}
… //else if部分可以出現任意次
else //else部分並不是必需的,可以沒有
{
語句塊;
}

此種語法的含義是,從上往下判斷布爾表達式,某個布爾表達式為 true,則執行相應的語句塊,
並結束整個結構。當所有的布爾表達式都為 false 時,則執行 else 部分的語句塊。簡單來說,就是 N
條路中選擇一條路走,選擇了一條路其它的路就不能走了;所有的語句塊,最后只會執行一個。
例 3.5 在上面的例 3.2 中使用了雙分支形式的 if 語句,要么顯示及格,要么顯示不及格,如果
我們希望要經過判斷分別顯示優良中差,則需要使用多分支的 if 語句,代碼如下:

4.2、switch語句 

switch 是多路選擇語句,它根據某個值來使程序多個分支中選擇一個用於執行。與 if 語句不同
的是,if 可以說任意情況下都可以使用,而 switch 一般只用於等值判斷,在某些時候可以簡化 if 語
句,並且提高執行效率。
C#中 switch 語句的語法格式如下:

switch(表達式)
{
case 【常量表達式】:語句塊;break;
case 【常量表達式】:語句塊;break;
…
case 【常量表達式】:語句塊;break;
default:語句塊;break; 
}

語法的含義是,計算表達式的值,跟每個 case 后的【常量表達式】比較,當表達式的值等於某個
case 后【常量表達式】的值時,執行該 case 后的語句塊。如果表達式的值與所有 case 后的【常量表
達式】都不相等時,則執行 default 后的語句塊。

switch 語句的使用過程需要注意以下幾點問題:

  1.  每個 case 后的【常量表達式】的值必須是與“表達式”的類型相同的一個常量,不能是
  2. 變量;
  3.  多個 case 后指定的常量值不能出現重復;
  4.  default 語句是可選的,一個 switch 語句中最多只能有一個 default 語句;
  5.  case 和 default 語句先后順序允許變動,一般 default 語句放在最后寫;
  6.  當 case 后面有處理語句時,break 將不能省略。否則,將提示錯誤。

示例1:

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

namespace C33
{
    class Program
    {
        static void Main(string[] args)
        {
            String sex = Console.ReadLine();

            switch (sex)
            {
                case   "":
                    Console.WriteLine("輸入正確,性別是男");
                    break;
                case "":
                    Console.WriteLine("輸入正確,性別是女");
                    break;
                default:
                    Console.WriteLine("輸入錯誤");
                    break;
            }

        }
    }
}

輸出:

示例2:

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

namespace C34
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("請輸入月份:");
            int month = Convert.ToInt32(Console.ReadLine());
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine(31);
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    Console.WriteLine(30);
                    break;
                case 2:
                    Console.WriteLine(28);
                    break;
                default:
                    Console.WriteLine("輸入錯誤");
                    break;
            }
        }
    }
}

輸出:

 if與switch嵌套

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

namespace C35
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("第一個數:");
            double m = Convert.ToDouble(Console.ReadLine());

            Console.Write("運算符(+ - * /):");
            String mark = Console.ReadLine();

            Console.Write("第二個數:");
            double n = Convert.ToDouble(Console.ReadLine());

            switch (mark)
            {
                case "+":
                    Console.WriteLine("{0}+{1}={2}", m, n, m + n);
                    break;
                case "-":
                    Console.WriteLine("{0}-{1}={2}", m, n, m - n);
                    break;
                case "*":
                    Console.WriteLine("{0}x{1}={2}", m, n, m * n);
                    break;
                case "/":
                    if (n != 0)
                    {
                        Console.WriteLine("{0}/{1}={2}", m, n, m / n);
                    }
                    else
                    {
                        Console.WriteLine("除數不能為零。");
                    }
                    break;
                default:
                    Console.WriteLine("運算符錯誤,只支持+-*/運算。");
                    break;
            }
        }
    }
}

五、課后作業

 5.1、完成第1章、第2章所有課后練習

5.2、完成第3章所有課后練習


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM