1. 程式人生 > >C#語言學習筆記(二):變量、數據類型和運算符

C#語言學習筆記(二):變量、數據類型和運算符

tex ear 布爾值 全部 exchange result 轉換成 lis int

變量、數據類型 技術分享圖片 技術分享圖片 技術分享圖片 給變量賦值,如果想指定float,在數字後面加上F/f;如果想指定decimal,在數字後面加上m。 技術分享圖片 技術分享圖片 0-9:48-57 A-Z:65-90 a-z:97-122 技術分享圖片 技術分享圖片 技術分享圖片 技術分享圖片 字符串格式化輸出 技術分享圖片 我們在使用Console.writeLine時,可以使用占位符格式化字符串 在字符串中使用{}包裹一個下標,來表示後面的變量 下標不能超過變量個數-1 數據轉換 技術分享圖片 隱式類型轉換,造成的信息丟失,不容易排查 顯示類型轉換,造成的信息丟失,會報錯且容易排查 顯示類型轉換的典型例子:高精度轉換為低精度,父類與派生類之間的轉換 convert轉換,用於基本類型到基本類型之間的轉換 Parse轉換,用於字符串到特定類型之間的轉換,功能更強大,可轉IP地址 技術分享圖片
強制轉換符,用於數值類型、派生關系之間的轉換 char字符類型也是數值類型,ASC碼表 技術分享圖片 技術分享圖片
using System.Text;
using System.Threading.Tasks;
namespace w1d3_namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 基本數據類型
            //基本類型
            //整型
            int num = 10;
            
long longNum = 10; Console.WriteLine(longNum); //小數型 float floatNum = 10.0f; double doubleNum = 10.0; decimal decimalNum = 10.0m; //字符 char cha = a; string str = "abc";//引用類型,是一種特殊的引用類型 //布爾 bool
isTrue = true; bool isFalse = false; Console.WriteLine("sizeof(int)=" + sizeof(int)); Console.WriteLine("sizeof(long)=" + sizeof(long)); Console.WriteLine("sizeof(float)=" + sizeof(float)); Console.WriteLine("sizeof(double)=" + sizeof(double)); Console.WriteLine("sizeof(decimal)=" + sizeof(decimal)); Console.WriteLine("sizeof(char)=" + sizeof(char)); //Console.WriteLine(sizeof(string));不能打出字符串,因為是引用類型 Console.WriteLine("sizeof(bool)=" + sizeof(bool)); #endregion #region 基本數據類型練習 double num1 = 36.6; double num2 = 36.6f; Console.WriteLine("num1=" + num1); Console.WriteLine("num2=" + num2); Console.WriteLine("請輸入你的年齡"); string myYear; myYear = Console.ReadLine(); Console.WriteLine("十年後你的年齡" + (10 + myYear)); //小明的數學考試成績是80,語文考試成績是78,英文的考試成績是98,請用變量描述 //數據 //小明 變量 數據類型 字符串 變量名 name //數學考試成績 變量 字符串 變量名mathSorce //語文考試成績 變量 字符串 變量名chineseSorce //英文考試成績 變量 字符串 變量名englishSorce Console.WriteLine("請輸入你的名字"); string name = Console.ReadLine(); Console.WriteLine("請輸入你的數學考試成績"); string mathSorce = Console.ReadLine(); Console.WriteLine("請輸入你的語文考試成績"); string chineseSorce = Console.ReadLine(); Console.WriteLine("請輸入你的英文考試成績"); string englishSorce = Console.ReadLine(); Console.WriteLine(name + "數學考試成績:" + mathSorce + ",語文考試成績:" + chineseSorce + ",英文考試成績:" + englishSorce); //字符類型 int i1 = 65; int i2 = A; char c1 = A; char c2 = (char)65; Console.WriteLine("i1=" + i1); Console.WriteLine("i2=" + i2); Console.WriteLine("c1=" + c1); Console.WriteLine("c2=" + c2); Console.WriteLine("依次輸入學生姓名"); Console.WriteLine("C#語言成績"); string cSharp = Console.ReadLine(); int cSharp1 = Convert.ToInt16(cSharp);//強制類型轉換 Console.WriteLine("Unity的成績"); string unity = Console.ReadLine(); int unity1 = Convert.ToInt16(unity); Console.WriteLine("你的總分是" + (cSharp1 + unity1)); #endregion #region 運算符 //運算符 //用來處理數據的一種邏輯 #endregion #region 算數運算符 //+,-,*,/,% //用於數值類型的算術運算 // + 可以用於字符串的連接, - 不可以 int math = 80; int english = 78; int chinese = 90; int sum = math + english + chinese; Console.WriteLine("總分是" + sum); Console.WriteLine("數學比英語多" + (math - english));//減號不可以連接字符串,要用括號括起來先計算 Console.WriteLine("四五得" + (4 * 5));//建議所有的運算,全部都用括號括起來,不要記憶運算符的優先級,用括號 Console.WriteLine("二十除以六" + (20 / 6));//如果你需要得到一個高精度的結果,請使用高精度數據參與運算 Console.WriteLine("二十除以六" + (20f / 6f));//float Console.WriteLine("二十除以六" + (20.0 / 6.0));//double Console.WriteLine("二十余六" + (20 % 6));//求余用整數 #endregion #region 算數運算符的練習 //string guestName; //string guestYear; //Console.WriteLine("請輸入你的名字"); //string guestName = Console.ReadLine(); // Console.WriteLine("請輸入你的年齡"); //string guestYear = Console.ReadLine(); //Console.WriteLine(guestName + "+" + guestYear + "歲了"); Console.WriteLine("請輸入你的姓名"); string guestName = Console.ReadLine(); Console.WriteLine("請輸入你的年齡"); string guestYear = Console.ReadLine(); Console.WriteLine("請輸入你的郵箱"); string guestMail = Console.ReadLine(); Console.WriteLine("請輸入你的家庭住址"); string guestAdress = Console.ReadLine(); Console.WriteLine("請輸入你的期望工資"); string guestManey = Console.ReadLine(); Console.WriteLine(guestName + "," + guestYear + "," + guestMail + "," + guestAdress + "," + guestManey); #endregion #region 賦值運算符 //=,+=,-=,*=,/= //= 是將右邊的值(數據)賦值給左邊的變量,類型必須一致 //+= int age = 18; age = age + 10; Console.WriteLine(age); age += 10;//數據自身參與運算,且只有一個運算 Console.WriteLine(age); int age1; age1 = (int)Convert.ToDouble(age) / 10; Console.WriteLine(age); age /= 10; Console.WriteLine(age); age = age % 10; Console.WriteLine(age); age %= 10; Console.WriteLine(age); #endregion #region 自增減運算符 //++ -- int age = 0; age = age + 1; age += 1; //++ 相當於變量自身做加一的運算 //++ 相當於變量自身做減一的運算 //當自增減運算符作為前綴時,先運算,再將結果交給使用者 //當自增減運算符作為後綴時,先將數據交給使用者,後運算 //如果你想回避前後綴問題,使用自增減運算符時,就只運算,不參與使用 age = 0; Console.WriteLine(++age);//age++;先運算後加減 //運算結果是1,打印出來是1 age = 0; Console.WriteLine(age++);//++age;先加減後運算 //運算結果是1,打印出來是0 #endregion #region 關系運算符 雙目運算符 //>,<,==,!=,>=,<= //用於數值類型之間的關系比較 //用於處理左右兩邊數據的關系 //如果關系達成,返回一個布爾值為true值 //如果關系不達成,返回一個布爾值為false值 bool result = true; result = 10 > 20; Console.WriteLine(result); #endregion #region 運算符練習 //小李有20個蘋果,小王有10個蘋果,他們想和對方交換,請實現 int xiaoLi = 20; int xiaoWang = 10; int exchange = 0; Console.WriteLine("小李的蘋果有" + xiaoLi + ",小王的蘋果有" + xiaoWang); //如果我們要交換兩個變量的數據 //先將一方的數據保存到一個臨時變量 exchange = xiaoLi; xiaoLi = xiaoWang; xiaoWang = exchange; Console.WriteLine("交換後"); //Console.WriteLine("小李的蘋果有" + xiaoLi + ",小王的蘋果有" + xiaoWang); Console.WriteLine("小李的蘋果有{0},小王的蘋果有{1}", xiaoLi, xiaoWang); //計算任意三門成績的總分、平均分 Console.WriteLine("請輸入第一門的成績"); string score1 = Console.ReadLine(); double score11 = Convert.ToDouble(score1); Console.WriteLine("請輸入第二門的成績"); string score2 = Console.ReadLine(); double score22 = Convert.ToDouble(score2); Console.WriteLine("請輸入第三門的成績"); string score3 = Console.ReadLine(); double score33 = Convert.ToDouble(score3); double sum = score11 + score22 + score33; double aver = sum / 3; //Console.WriteLine("總分是" + sum + ",平均分是" + aver); //我們在使用Console.writeLine時,可以使用占位符格式化字符串 //在字符串中使用{}包裹一個下標,來表示後面的變量 //下標不能超過變量個數-1 Console.WriteLine("總分是{0},平均分是{1}", sum, aver);//字符串格式化輸出 //計算一個半徑為5的圓的面積和周長 //float r = 5; //float PI = 3.1415926f; //float perimeter = 2 * r * PI; //float area = PI * r * r; Console.WriteLine("請輸入半徑"); string radius = Console.ReadLine(); double r = Convert.ToDouble(radius); double PI = 3.1415926f; double perimeter = 2 * r * PI; double area = PI * r * r; //Console.WriteLine("圓的面積是" + area + "圓的周長是" + perimeter); Console.WriteLine("圓的面積是{0},圓的周長是{1}", area, perimeter); //某商店T恤的價格為285元/件,褲子的價格為720元/條,小李在該店買了2件T恤和3條褲子,請計算小李該付多少錢?打3.8折後呢? Console.WriteLine("T恤的價格為285元/件,褲子的價格為720元/條"); Console.WriteLine("請輸入T恤件數"); string tShirt = Console.ReadLine(); double tShirt1 = Convert.ToDouble(tShirt); Console.WriteLine("請輸入褲子件數"); string pants = Console.ReadLine(); double pants1 = Convert.ToDouble(pants); double price = tShirt1 * 285 + pants1 * 720; double discount = tShirt1 * 285 + pants1 * 720; //Console.WriteLine("原價為" + price + ",打折為" + discount); Console.WriteLine("原價是{0},打折是{1}", price, discount); #endregion #region 字符串格式化輸出 //我們在使用Console.writeLine時,可以使用占位符格式化字符串 //在字符串中使用{ } //包裹一個下標,來表示後面的變量 //下標不能超過變量個數 - 1 #endregion #region 數據類型轉換 //隱式轉換 //系統默認發生的,將低精度向高精度的類型轉換 //如果你需要結果是高精度,數據源盡量保持高精度 //顯式轉換 //我們調用一些指令對數據進行轉換 #endregion #region Convert //Convert(顯式轉換) //用於所有基本類型之間的轉換 //用戶輸入姓名,語文,數學,英語的成績,最後在控制臺顯示:XXX,你的總成績為XX分,平均分是XX分 Console.WriteLine("請輸入您的姓名"); string name = Console.ReadLine(); Console.WriteLine("請輸入您的語文成績"); string inputChinese = Console.ReadLine(); float chinese = Convert.ToSingle(inputChinese);//float的顯式轉換用single表示 Console.WriteLine("請輸入您的數學成績"); float math = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("請輸入您的英語成績"); float english = Convert.ToSingle(Console.ReadLine()); float sum = chinese + math + english; float aver = sum / 3; Console.WriteLine("您的總分是{0},平均分是{1}", sum, aver); Console.WriteLine(1.1 + 2.2 + 3.3); Console.WriteLine(1.1 + 2.2 + 3.3f);//隱式轉換數據丟失 Console.WriteLine(1.1 + 2.2 + Convert.ToSingle(3.3f));//顯式轉換數據丟失 int age1 = Convert.ToInt16(A); Console.WriteLine(age1); //int age2 = Convert.ToInt16("AAA");//Convert的值必須可轉 //Console.WriteLine(age2); Console.WriteLine(111 + 111 + "");// Console.WriteLine("" + 111 + 111);//字符串轉換,如果加號左邊是字符,默認將右邊的類型轉成字符串 #endregion #region Parse轉換 //將字符串轉換成一個特定類型 //由特定類型調用 //提示用戶輸入一個數字,接收並計算這個數字的1.5倍 Console.WriteLine("請輸入一個整數"); int num1 = int.Parse(Console.ReadLine()); Console.WriteLine(num1 * 1.5f); Console.WriteLine("請輸入一個數字"); float num2 = float.Parse(Console.ReadLine()); Console.WriteLine(num2 * 1.5f); Console.WriteLine("請輸入一個數字"); double num3 = double.Parse(Console.ReadLine()); Console.WriteLine(num3 * 1.5f); Console.WriteLine("請輸入一個數字"); decimal num4 = decimal.Parse(Console.ReadLine()); Console.WriteLine(num4 * 1.5m); #endregion #region 強制轉換符 //()把需要轉到的類型填在括號裏,把需要轉換的數據放在括號後面 //用於數值類型(整型,浮點型,char)之間的轉換 //用於派生(繼承)關系之間的轉換 int a = (int)a; Console.WriteLine(a); int b = (int)1.35f; Console.WriteLine(b);//強制轉換,精度丟失 //將一個char轉換成float float c = (float)c; Console.WriteLine(c); // float d = (int)d; Console.WriteLine(d); #endregion } } }

C#語言學習筆記(二):變量、數據類型和運算符