[技術類 - C# 學習誌 - C#面試考題 ] 變數、資料型態、運算子 之二

行動版 for , 瀏覽人次: 3771  , SSL Connection SSL
  • // 程式功能 : 計算三角形面積
    // 先列印出二行文字請使用者輸入三角形的底及高(整數)
    // 計算後列印出三角形的面積大小

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //程式有多種作法
                Console.WriteLine(“請輸入三角形的底及高");
                int a = int.Parse(Console.ReadLine());
                int b = int.Parse(Console.ReadLine());
                Console.WriteLine(“結果是:" + ((double)(a*b)/2));
                //string botom = Console.ReadLine();
                //string height = Console.ReadLine();
                //int Answer = int.Parse(botom) * int.Parse(height) / 2;
                //Console.WriteLine(“結果是:" + Answer);
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 輸入二個數字
    // 列出二數之前的關係 (比較子)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //輸入二數
                Console.Write(“輸入第一個數字:");
                int i = int.Parse(Console.ReadLine());
                //列出結果
                Console.WriteLine(i + " == " + j + " 的結果為" + (i == j));
                Console.WriteLine(i + " != " + j + " 的結果為" + (i != j));
                Console.WriteLine(i + " > " + j + " 的結果為" + (i > j));
                Console.WriteLine(i + " < " + j + " 的結果為" + (i < j));
                Console.WriteLine(i + " >= " + j + " 的結果為" + (i >= j));
                Console.WriteLine(i + " <= " + j + " 的結果為" + (i <= j));
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 先請使用者輸入一個數字
    // 判斷該數字是否大於5
    // 大於5 則列印true ,否則列印false
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //輸入數字
                Console.Write(“輸入數字:");
                int i = int.Parse(Console.ReadLine());
                //列出結果
                Console.WriteLine(i + “>5 的結果為" + (i > 5));
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 計算正方形面積
    // 請使用者輸入正方形邊長(整數)
    // 計算後列印出正方形面積大小
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //輸入數字
                Console.Write(“輸入數字:");
                int area = int.Parse(Console.ReadLine());
                area *= area;
                //列出結果
                Console.WriteLine(“正方形面積為" + area.ToString());
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 遞增與遞減 之概念
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i = 0;
                int j;
                j = 3 + i++;
                Console.WriteLine(j);
                Console.WriteLine(i);
                //此時結果 j:3  i:1
                j = 3 + ++i;
                Console.WriteLine(j);
                Console.WriteLine(i);
                //此時結果 j:5  i:2
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 遞增與遞減之概念
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int i = 10, j = 0, x = 10, y = 0;
                j += ++i;
                Console.WriteLine(“i =" + i + “, j = " + j);
                //i:11  j:11
                y += x++;
                Console.WriteLine(“x =" + x + “, y = " + y);
                //x:11 y:10
                Console.WriteLine(“x =" + x++); // How about ++x ? => x:12
                //x:11
                Console.WriteLine(“x =" + x);
                //x:12
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 計算圓面積
    // 請使用者輸入圓半徑(整數)
    // 計算後列印出圓形面積大小
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                const double pi = 3.1415926;
                double n1 = double.Parse(Console.ReadLine());
                double area = n1 * n1 * pi;
                Console.WriteLine(“圓形面積大小為:"+area.ToString());
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–

    //  列舉型別 1 – 列舉型別不可以在方法內宣告

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                language my_a = language.French;
                Console.WriteLine(“my_a為:" + my_a);
                long a = (long)language.French;
                Console.WriteLine(“a為:" + a);
                Console.ReadLine();
            }
            enum language : long
            {
                English = 0,  //左邊是symbol , 右邊是value
                Chinese = 1,  //左邊是symbol , 右邊是value
                French = 2,  //左邊是symbol , 右邊是value
                Japanease = 3  //左邊是symbol , 右邊是value
            }
        }
    }
    ——————————————————————————————————————–
    // 列舉型別 2 – 列舉型別不可以在方法內宣告
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            enum studentID : int
            {
                alice = 12,
                bob = 24
            }
            static void Main(string[] args)
            {
                Console.WriteLine(studentID.alice + " 的座號為: " +
                (int)studentID.alice);
                Console.WriteLine(studentID.bob + " 的座號為: " +
                (int)studentID.bob);
                Console.WriteLine(“座號12 號的是: " + (studentID)12);
                Console.WriteLine(“座號24 號的是: " + (studentID)24);
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    //  列舉型別 3 – 列舉型別不可以在方法內宣告
    // 列印出星期一到日各為一星期的第幾天
    // 以星期日為第一天
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            enum which_day : int
            {
                sunday = 1,
                monday = 2,
                tuesday = 3,
                wednesday = 4,
                thursday = 5,
                friday = 6,
                saturday = 7
            }
            static void Main(string[] args)
            {
                Console.WriteLine(which_day.sunday + " 為第: " + (int)which_day.sunday + “天");
                Console.WriteLine(which_day.monday + " 為第: " + (int)which_day.monday + “天");
                Console.WriteLine(which_day.tuesday + " 為第: " + (int)which_day.tuesday + “天");
                Console.WriteLine(which_day.wednesday + " 為第: " + (int)which_day.wednesday + “天");
                Console.WriteLine(which_day.thursday + " 為第: " + (int)which_day.thursday + “天");
                Console.WriteLine(which_day.friday + " 為第: " + (int)which_day.friday + “天");
                Console.WriteLine(which_day.saturday + " 為第: " + (int)which_day.saturday + “天");
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–

    // 利用struct 建立student 基本資料

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            struct student  //struct結構   //新宣告的type – 下面那三個欄位,相關性很高,可一次宣告就好
            {
                public string name;  //一種欄位
                public int ID;  //一種欄位
                public int age;  //一種欄位
            }
            static void Main(string[] args)
            {
                student new_student;  //student 為 struct結構
                new_student.age = 25;
                new_student.ID = 99;
                new_student.name = “Anson";
                Console.WriteLine(“學生姓名: " + new_student.name + “\n" + “學生ID: " + new_student.ID);
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 利用結構自訂通訊錄資料型態
    // 列印三行請使用者輸入姓名、年齡及電話
    // 輸入完後列印出使用者的相關資料
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            struct student  //struct結構   //新宣告的type – 下面那三個欄位,相關性很高,可一次宣告就好
            {
                public string name;  //一種欄位
                public int age;  //一種欄位
                public string tel;  //一種欄位
            }
            static void Main(string[] args)
            {
                student new_student;  //student 為 struct結構
                Console.WriteLine(“請輸入姓名: “);
                new_student.name = Console.ReadLine();
                Console.WriteLine(“請輸入年齡: “);
                new_student.age = int.Parse(Console.ReadLine());
                Console.WriteLine(“請輸入電話: “);
                new_student.tel = Console.ReadLine();
                Console.WriteLine(“資料為:\n 姓名:" + new_student.name + “\n 年齡:" + new_student.age + “\n 電話:" + new_student.tel);
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    // 亂數之概念
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random rnd = new Random();
                int a =rnd.Next(3, 10);  //產生3~9之間的亂數  //亂數的值會大於等於最小值,小於最大值
                Console.WriteLine(a);
                //先用亂數產生一個1 ~ 3 之間的數
                //讓使用者輸入一個數字
                //使用者猜對則列印true 猜錯印出false
                Random rnd2 = new Random();
                int b = rnd.Next(1, 4);   //亂數的值會大於等於最小值,小於最大值
                Console.WriteLine(“請輸入一個1~3之間的數字");
                int c = int.Parse(Console.ReadLine());
                Console.WriteLine(b==c); //先不用寫 if else…
                Console.ReadLine();
            }
        }
    }
回 文章列表頁