其他路徑:
CSDN: https://blog.csdn.net/wodehao0808
微信公衆號:程序喵星人
更多資源和視頻教程,QQ:1902686547
3. 變量和表達式
3.1 變量
計算機程序的運行其實就是對數據的操作,數據是什麼?比如數字,文字,圖片這些在計算機中都是數據,那麼數據怎麼在計算機中存儲呢?
答案:通過變量
你可以把計算機內存中的變量,當成一個盒子,盒子裏面存儲着東西,可以放入或者取出。
3.1.1 變量的聲明
聲明變量需要指定類型和變量名
<type> <name>;
type表示使用什麼類型的盒子,來存儲數據
name表示存儲這個盒子的名字
實例:(每一個聲明都是一條語句,語句以;結束)
int age;
int hp;
string name;
3.1.1.1 Example:
變量的聲明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 變量的聲明
namespace Lesson_2_1
{
class Program
{
static void Main(string[] args)
{
int iAge = 20;
int iHp = 60;
string strName = "CShape";
Console.WriteLine(strName);
Console.ReadKey();
}
}
}
3.1.2 變量的類型
3.1.2.1 整數
3.1.2.2 小數
3.1.2.3 非數值類型
3.1.2.4 Example:整數、小數、非數值類型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 整數,小數,非數值類型
namespace Lesson_2_2
{
class Program
{
static void Main(string[] args)
{
// 整數
byte bytByte = 20;
int iScore = 6000;
long lngCount = 10000003600;
Console.WriteLine("byte:{0} int:{1} long:{2}", bytByte, iScore, lngCount );
// 小數
float fValue = 13.6f; // 後綴 f 表示是浮點數
double dblValue = 36.954;
Console.WriteLine("float:{0} double:{1}", fValue, dblValue);
// 非數值類型
char chChar = 'a'; // char 使用 單引號 表示一個字符
string strString = "a"; // 使用 雙引號 字符串
string strString2 = "OK";
bool bResult = true; // bool 類型只有 false 和 true
Console.WriteLine("char:{0} string:{1} string2:{2} bool:{3}", chChar, strString, strString2, bResult);
Console.ReadKey();
}
}
}
3.1.3 變量的命名
變量的命名,遵循 標識符 的命名規則。
遵守命名規範可以讓程序結構更加清晰,更易於閱讀。
規範:第一個單詞以小寫字母開頭,以後每個單詞的首字母大寫。
變量的命名遵守Camel命名法(駝峯命名法)。首字母小寫,以後每個單詞的首字母大寫。
3.1.4 變量的練習
練習:定義一些變量存儲一個主角的信息,輸出到控制檯;
名字、血量、等級、經驗值
3.1.5 Example: 練習定義變量存儲主角的信息,轉義字符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習定義變量存儲主角的信息,轉義字符
namespace Lesson_2_3
{
class Program
{
static void Main(string[] args)
{
string strName = "張三";
int iHp = 100;
int iLevel = 16;
float fExp = 163.5f;
Console.WriteLine("主角的名字是:{0} 血量:{1} 等級:{2} 經驗值:{3}", strName, iHp, iLevel, fExp);
// \n 換行
Console.WriteLine("2--主角的名字是:{0} \n血量:{1} \n等級:{2} \n經驗值:{3}", strName, iHp, iLevel, fExp);
// \" 在字符串中使用雙引號
Console.WriteLine("3--主角的名字是:\"{0}\" \n血量:{1} \n等級:{2} \n經驗值:{3}", strName, iHp, iLevel, fExp);
// \\ 使用斜槓
Console.WriteLine("4--主角的名字是:\\{0}\\ \n血量:{1} \n等級:{2} \n經驗值:{3}", strName, iHp, iLevel, fExp);
// \t 製表符
Console.WriteLine("4--主角的名字是:\t{0} \n血量:{1} \n等級:{2} \n經驗值:{3}", strName, iHp, iLevel, fExp);
Console.ReadKey();
}
}
}
3.1.6 字面值
字面值:表示文本和數字的。
3.1.7 char和string
char表示一個字符,字母 數字 @#¥%……&*()一個漢字。
string是一個char的數組,可以把string認爲字符的集合。
3.1.8 轉義字符列表
轉義字符是有特殊功能的字符
3.1.9 字符的Unicode值的作用
Unicode是一個16進制的數字,表示這個字符在內存中以哪個數字存儲。 也可以使用Unicode來代表一個轉義字符 (\u加上十六進制值),例如:
"I\'s siki!"
"I\u0027s siki!"
3.1.10 使用@不識別轉義字符
如果我們不想去識別字符串中的轉義字符,可以在字符串前面加一個@符號(除了雙引號其他轉義字符都不在識別,如果要顯示一個雙引號,則需要使用兩個雙引號)。
string str = @"Hello "" this "" Ok"; // 使用@時,輸出雙引號
使用兩個引號表示一個引號
舉例:
"I'm a good. \n You are good!",
@字符的兩個作用示例:
1,默認一個字符串的定義是放在一行的,如果想要佔用多行
2,用字符串表示路徑
"c:\\xxx\\xx\xxx.doc"
使用@"c:\xxx\xx\xxx.doc"更能讀懂
3.1.11 變量的聲明和賦值
變量的聲明
int age ;
變量的賦值
age = 25;
變量的聲明和賦值可以放在一個語句中
int age = 25;
3.1.12 多重聲明和賦值
我們可以使用一條語句聲明多個類型一樣的變量。
string name1,name2;
在多變量聲明中,可以在變量後面跟上=,對其中的一個變量或者部分或者全部變量進行初始化。
string strName = "Li", strName2 = "Ry";
int iMax = 100, iCount;
3.1.13 Example使用@不識別轉義字符;多重聲明和賦值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 使用@不識別轉義字符;多重聲明和賦值;
namespace Lesson_2_4
{
class Program
{
static void Main(string[] args)
{
string str = "I'm a good. \n You are good!";
Console.WriteLine(str);
// 使用@, \n失效,當作是字符串直接輸出
string str2 = @"I'm a good. \n You are good!";
Console.WriteLine(str2);
// 使用@時,使用雙引號方法是:使用兩個雙引號輸出一個雙引號
string str3 = @"Hello ""this"" Ok";
Console.WriteLine(str3);
// 使用@,把一個字符串定義在多行
string str4 = @"My
name is 李四,
Who are you?";
Console.WriteLine(str4);
// 使用@,表示路徑
string str5 = "C:\\xxx\\xxx\\xx.doc";
string str6 = @"C:\xxx\xxx\xx.doc"; // 不需要 \\ , 路徑清晰明瞭
Console.WriteLine("str5 = " + str5);
Console.WriteLine("str6 = " + str6);
// Ctr + k + c鍵,快速註釋
// Ctr + k + u鍵,取消註釋
// 多重聲明和賦值
int iHp, iMp = 100, iLevel = 10;
Console.WriteLine(iLevel);
Console.ReadKey();
}
}
}
3.1.14 注意事項
變量在使用之前必須初始化。
怎麼判斷變量有沒有使用,當你從變量的盒子裏面取東西的時候就是要使用這個變量的時候,初始化就是先往這個盒子裏面放入東西,才能去取。
第一次給變量賦值,就叫做初始化。
變量類型,可以理解爲: 變量類型決定了盒子的最大容量,即盒子最大多大。
3.2 表達式
把變量和字面值和運算符組合起來,就是表達式。
運算符分類:
一元運算符 處理一個操作數
二元運算符 處理兩個操作數
三元運算符 處理三個操作數
3.2.1 數學運算符
數學運算返回結果:
1.當兩邊的操作數類型一致的時候,返回的結果跟操作數的類型一樣。
2.當兩邊的操作數類型不一致的時候,返回的結果跟類型大的操作數保持一致,這樣做編譯器是爲了保證結果可以存的下,因爲如果其中有一個類型大的操作數,很可能結果也是一個比較大的數值這樣小類型是存不下的。
char 可以用來做數學運算;
char 變量實際在內存中儲存的是數字;
+ 號,可以用來連接兩個字符串;
例如:
string str1 = "abc";
string str2 = "mn";
int icount = 5;
string str2 = str1 + str2; // "abcmn"
string str3 = str1 + icount; // "abc5"
3.2.1.1 Example: 數學運算符+-*/%等
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 數學運算符+-*/%等
namespace Lesson_2_5
{
class Program
{
static void Main(string[] args)
{
// +-*/%
int iNum1 = 45, iNum2 = 15;
int iRes1 = iNum1 + iNum2; // 加
int iRes2 = iNum1 - iNum2; // 減
int iRes3 = iNum1 * iNum2; // 乘
int iRes4 = iNum1 / iNum2; // 除
int iRes5 = iNum1 % iNum2; // 取餘
Console.WriteLine("加 = {0}", iRes1);
Console.WriteLine("減 = {0}", iRes2);
Console.WriteLine("乘 = {0}", iRes3);
Console.WriteLine("除 = {0}", iRes4);
Console.WriteLine("取餘 = {0}", iRes5);
// 帶小數的取餘
double iRes6 = 132.8 % 3; // 注意的是,操作數中最大類型是 double 類型,所以返回結果也是double類型,所以 iRes6 是double類型。
Console.WriteLine("iRes6 = " + iRes6 ); // 加號操作,一個是字符串,一個是數值,會將數值轉換爲字符串,然後連接起來。
// 兩個操作數不一致的加法運算結果
double iRes7 = 12.3 + iNum1; // 兩個操作數不一致,返回結果爲操作數類型的最大類型,double類型大於int類型,所以返回double類型數值;
Console.WriteLine("iRes7 = " + iRes7);
// 加號:兩個字符串,則連接兩個字符串成一個字符串
string str1 = "Hello";
string str2 = "World";
string str = str1 + str2; // 兩個字符串連接成一個字符串
Console.WriteLine("str = " + str);
// 加號:一個是字符串,一個是數值,則將數值轉換爲字符串,然後連接成一個字符串
string str3 = "abc";
int iCount = 90;
string str4 = str3 + iCount; // "abc90"
string str5 = "" + iCount; // "90"
Console.WriteLine("str4 = {0}, str5 = {1}", str4, str5);
Console.ReadKey();
}
}
}
3.2.1.2 自增和自減
自增:++,讓操作數加1;
自減:--,讓操作數減1;
例如:
int n = 10;
int n2 = n++; // n2 值爲10,n值爲11
int m = 10;
int m2 = ++m; // m2值爲11,m值爲11
int i = 10;
int i2 = i--; // i2值爲10, i值爲9
int j = 10;
int j2 = --j; // j2值爲9,j值爲9
上面的示例可以看出,前自增或前自減(++m或--j)是先對操作數進行自增或自減操作,之後再進行運算;後自增或後自減(n++或i--)是先進行運算,之後再對操作數進行自增或自減操作。
3.2.1.3 Example:自增和自減
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 自增和自減
namespace Lesson_2_6
{
class Program
{
static void Main(string[] args)
{
// 前自增 -- 先對值進行加1,再進行其他運算。
int iNum = 10;
int iRet = ++iNum;
Console.WriteLine("前自增 = {0}, iNum = {1}", iRet, iNum);
// 後自增 -- 先進行其他運算,再對值加1。
int iNum2 = 10;
int iRet2 = iNum2++;
Console.WriteLine("後自增 = {0}, iNum2 = {1}", iRet2, iNum2);
// 前自減 -- 先對值進行減1,再進行其他運算。
int iNum3 = 10;
int iRet3 = --iNum3;
Console.WriteLine("前自減 = {0}, iNum3 = {1}", iRet3, iNum3);
// 後自減 -- 先進行其他運算,再對值減1。
int iNum4 = 10;
int iRet4 = iNum4--;
Console.WriteLine("後自減 = {0}, iNum4 = {1}", iRet4, iNum4);
Console.ReadKey();
}
}
}
3.2.2 接受用戶輸入
接受用戶輸入:
通過Console.ReadLine()方法,這個方法可以從鍵盤上讀取一行的字符串輸入,返回的是一個字符串。
3.2.2.1 Example:接受用戶輸入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 接受用戶的輸入
namespace Lesson_2_7
{
class Program
{
static void Main(string[] args)
{
// 接受用戶的輸入,返回的是一個字符串
Console.WriteLine("請輸入任意信息");
string str = Console.ReadLine(); // 讀取用戶輸入的一行信息,返回一個字符串
Console.WriteLine("用戶輸入的信息是 = " + str);
// 字符串轉爲int
Console.WriteLine("請輸入一個數字:");
int iNum = Convert.ToInt32(Console.ReadLine()); // Convert.ToInt32 將值轉換爲 int類型
Console.WriteLine("輸入的數字是 = " + iNum);
// 字符串轉爲double
Console.WriteLine("請輸入一個小數:");
double dNum = Convert.ToDouble(Console.ReadLine()); // Convert. ToDouble將值轉換爲 double類型
Console.WriteLine("輸入的小數是 = " + dNum);
Console.ReadKey();
}
}
}
3.2.3 練習:從控制檯輸入兩個數值並求和
賦值運算:
=
返回值:
方法的調用和表達式都會有返回值,返回給程序。
用戶輸入:
可以通過Console.ReadLine()從控制檯接受一行用戶輸入的字符串。
類型轉換:
把一個整形數字的字符串轉換成一個整形:
string str = "123";
int i = Convert.ToInt32(str);
把一個浮點類型的字符串轉換成一個浮點類型:
string str = "12.67";
float f = Conver.ToFloat(str);
3.2.3.1 Example: 從控制檯輸入兩個數,並求和
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 從控制檯輸入兩個數,並求和
namespace Lesson_2_8
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入第一個整數:");
int iNum1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第二個整數:");
int iNum2 = Convert.ToInt32(Console.ReadLine());
int iSum = iNum1 + iNum2;
Console.WriteLine("輸入的數:{0} + {1} 的和爲:{2}", iNum1, iNum2, iSum);
Console.ReadKey();
}
}
}
3.2.4 賦值運算符
賦值運算符:
= 二元運算符 var1 = var2
+= 二元運算符 var1 += var2 相當於:var1 = var1 + var2;
-= 二元運算符 var1 -= var2 相當於:var1 = var1 - var2;
*= 二元運算符 var1 *= var2 相當於:var1 = var1 * var2;
/= 二元運算符 var1 /= var2 相當於:var1 = var1 / var2;
%= 二元運算符 var1 %= var2 相當於:var1 = var1 % var2;
3.2.4.1 Example: 賦值運算符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 賦值運算符
namespace Lesson_2_9
{
class Program
{
static void Main(string[] args)
{
int iNum = 30;
iNum += 10; // 相當於 iNum = iNum + 10; // 40
iNum -= 10; // 相當於 iNum = iNum - 10; // 30
iNum *= 10; // 相當於 iNum = iNum * 10; // 300
iNum /= 10; // 相當於 iNum = iNum / 10; // 30
iNum %= 10; // 相當於 iNum = iNum % 10; // 0
Console.WriteLine(iNum);
Console.ReadKey();
}
}
}
3.2.5 運算符優先級
括號可以用來重寫優先級,括號內的優先級最高;
例如:
int i = 10 + 10 * 2; // 30 // 先進行乘法,再進行加法運算
int j = (10 + 10 ) * 2; // 40 // 先進行加法運算,再進行乘法運算
3.2.5.1 Example:運算符優先級
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 運算符優先級
namespace Lesson_2_10
{
class Program
{
static void Main(string[] args)
{
int i = 10 + 90;
int i2 = 10 + 90 * 2; // 在程序的表達式中,按照優先級順序,先進行優先級高的運算
int i3 = (10 + 90) * 2; // 可以使用括號()來改變運算的優先級,括號的運算總是最先執行
Console.WriteLine("i = " + i);
Console.WriteLine("i2 = " + i2);
Console.WriteLine("i3 = " + i3);
Console.ReadKey();
}
}
}
3.3 練習題
1,在Main方法中定義變量,用這些變量存儲遊戲中一個敵人應該有的一些屬性,定義儘可能多的變量。
2,接受用戶輸入的兩個整數,存儲到兩個變量裏面,交換變量存儲的值。
3,編寫一個控制檯應用程序,要求用戶輸入4個int值,並顯示他們的乘積。
4,從鍵盤輸入一個三位的正整數,把百分位十分位個分位數字的相反順序輸出。
5,編寫一個程序,輸入梯形的上底 下底 和高 ,計算出來梯形的面積並顯示出來。
梯形的面積=(上底+下底)*高 /2
6,計算半徑爲n的圓的周長和麪積,n讓用戶輸入
周長=2n*PI
面積=n*n*PI
3.3.1 Example: 練習題
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習題
namespace Lesson_2_11
{
class Program
{
static void Main(string[] args)
{
/*
// example_1: 定義變量,用這些變量存儲遊戲中一個敵人應該有的一些屬性
int iHp = 100, iMp = 100;
float iAtk = 30.2f, iDef = 23.5f;
float dScore = 100000000;
*/
/*
// example_2: 接受用戶輸入的兩個整數,存儲到兩個變量裏面,交換變量存儲的值。
Console.WriteLine("請輸入第一個整數:");
int iNum1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第二個整數:");
int iNum2 = Convert.ToInt32(Console.ReadLine());
int iTemp = iNum1;
iNum1 = iNum2;
iNum2 = iTemp;
Console.WriteLine("交換後的變量,第一個整數:{0},第二個整數:{1}", iNum1, iNum2);
*/
/*
// example_3: 用戶輸入4個int值,並顯示他們的乘積
Console.WriteLine("請輸入第一個整數:");
int iValue1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第二個整數:");
int iValue2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第三個整數:");
int iValue3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第四個整數:");
int iValue4 = Convert.ToInt32(Console.ReadLine());
long lng = iValue1 * iValue2 * iValue3 * iValue4;
Console.WriteLine("輸入的四個整數的乘積是:" + lng);
*/
/*
// example_4: 輸入一個三位的正整數,把百分位十分位個分位數字的相反順序輸出。
Console.WriteLine("請輸入一個100到999的整數:");
int i = Convert.ToInt32(Console.ReadLine());
int iBai = i / 100; // 百位
int iShi = i / 10 % 10; // 十位
int iGe = i % 10; // 個位
Console.WriteLine("倒序輸出:{0}{1}{2}", iGe, iShi, iBai);
*/
/*
// example_5: 輸入梯形的上底 下底 和高 ,計算出來梯形的面積並顯示出來。
Console.WriteLine("請輸入梯形的上底:");
float f1 = Convert.ToSingle(Console.ReadLine()); // ToSingle 爲單精度浮點型 float
Console.WriteLine("請輸入梯形的下底:");
float f2 = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("請輸入梯形的高:");
float f3 = Convert.ToSingle(Console.ReadLine());
float f = (f1 + f2) * f3 / 2;
Console.WriteLine("梯形的面積是:" + f);
*/
// example_6: 計算半徑爲n的圓的周長和麪積,n讓用戶輸入
Console.WriteLine("請輸入圓的半徑");
double dR = Convert.ToDouble(Console.ReadLine()); // 半徑
double dLen = 2 * dR * 3.14; // 周長
double dM = dR * dR * 3.14; // 面積
Console.WriteLine("圓的周長是:{0},圓的面積是:{1},圓的半徑是:{2}", dLen, dM, dR);
Console.ReadKey();
}
}
}