iT邦幫忙

0

C# 入門筆記03(封裝)

變數

C# 中提供的基本值型別可以分為:

定義變數
C# 中變數定義的語法是:data_type
因此data_type必須是一個有效的 C# 資料型別,包括:char,int,float,double或任何使用者定義的資料型別

以下sample使用各種型別:

    class Program
    {
        static void Main(string[] args)
        {
            short a;
            int b;
            double c;

            a = 10;
            b = 20;
            c = a + b;
            Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
            Console.ReadLine();
        }
    }

輸出的值為:

a = 10, b = 20, c = 30

C#編程題型:

聲明兩個變量:int n1=10,n2=20;要求將兩個變量交換,最后輸出n1為20,n2為10。
提示:不使用第三個變量如何交換?

    class Program
    {

        static void Main(string[] args)
        {
            int n1 = 10, n2 = 20;
            n1 = n2 - n1;  // n1 = 10
            n2 = n2 - n1;  // n2 = 10
            n1 = n1 + n2;  // n1 = 20
        }
    }

解:
swap有兩個參數A,B,在參數中使用ref將n1和n2交換

    class Program
    {
        public static void swap(ref int A, ref int B)
        {
            A = B - A;  // 10
            B = B - A;  // 10
            A = A + B;  // 20
        }

        static void Main(string[] args)
        {
            int n1 = 10, n2 = 20;
            swap(ref n1, ref n2);
            Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);
        }
    }

輸出值:

n1 = 20, n2 = 10

若要在特定平台上獲取型別或變數的確切大小,可以使用sizeof方法。 表示式sizeof(type)產生物件或型別的儲存大小(以位元組為單位)。以下是在任何機器上獲取double型別的大小的範例:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("test: {0}", sizeof(double));
            Console.ReadLine();
        }
    }

輸出值為:

test: 8

按鈕點擊次數小遊戲

先在工具箱拉取兩個Button、一個Label的欄位,樣式可自行更改
再Label的屬性中,將(name)欄位更改為timesLabel
先宣告一個整數numes = 0;
因為第一個按鈕要記錄點擊次數,因此使用numes++;
第二顆按鈕要讓數值規0,因此使用numes = 0;

完整程式碼如下:

    public partial class Form1 : Form
    {
        int numes = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            numes++;
            timesLabel.Text = "一共點擊了" + numes + "次";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            numes = 0;
            timesLabel.Text = "一共點擊了" + numes + "次";
        }
    }

九九乘法表
\t => 類似tab鍵
\r\n => 迴圈執行完自動換行

class Program
    {
        static void Main(string[] args)
        {
            int num = 9;
            for (int i = 0; i < num - 1; i++)
            {
                for(int j = 0; j < num; j++)
                {
                    Console.Write($"{i + 2} * {j + 1} = {(i + 2) * (j + 1)}\t");                  
                }
                Console.WriteLine("\r\n");
            }
            Console.ReadLine();
        }
    }

編譯出來的值:
圖


星星數量
首先,先建立Form表單並創建為下圖樣式

在NurmericUpDown的按鈕上將屬性名稱改為getStars
接著在function內輸入int stars = getStars.Value;
這時系統會告知無法將類型"decimal"轉換成"int",我們只要在值的前方加入(int),即可強制轉型.

 private void button1_Click(object sender, EventArgs e)
        {
            int stars =(int) getStars.Value; //(int)強制轉型
            string result = "";

            for(int i = 1; i <= stars; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    result += "*";
                }
                result += "\r\n";
            }
            MessageBox.Show(result);
        }

編譯出來後的成果:


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言