C#基礎程序員
分支: switch
switch(表達式)
{
case 具體值1:
語句;
....;
break;
case 具體值2:
語句;
....;
break;
.....數組
default:
語句;
....;
break;遞歸
}
案例:
string s = Console.ReadLine();
switch (s)
{
case "1":
Console.WriteLine("你選擇的是庫存管理模塊");
break;
case "2":
Console.WriteLine("你選擇的是入庫管理模塊");
break;
case "3":
Console.WriteLine("你選擇的是出庫管理模塊");
break;
case "4":
Console.WriteLine("你選擇的是加工管理模塊");
break;
case "5":
Console.WriteLine("你選擇的是退出系統");
break;索引
default:
Console.WriteLine("輸入的指令不可設別,請從新輸入");
break;隊列
}
循環: whileci
初始條件
while(循環條件)
{
循環體;
狀態的改變;
}
案例:
int i=0;
while(i<10)
{
Console.WriteLine("您好@");
i++;
}
它是從下面演變過來的:
//int i = 0;
//for (; i < 10; )
//{
// Console.WriteLine("您好!");
// i++;
//}string
foreach 通常用來循環遍歷數組(或集合)的。it
用來從頭至尾遍歷,不涉及到下標或計數;並且循環過程當中,不能修改數組或集合中的值。
foreach(變量類型 變量 in 數組或集合)
{
Console.WriteLine(變量);
}io
數組:鋸齒數組 數組的數組
一、定義
1.定義數組的數組:
int[][] a = new int[3][];
2.定義一維數組:
int[] a1 = new int[5] { 10, 20, 30, 40, 50 };
int[] a2 = new int[2] { 60, 70 };
int[] a3 = new int[3] { 80, 90, 100 };
3.把一維數組放到數組的數組中。
a[0] = a1;
a[1] = a2;
a[2] = a3;table
二、操做某個元素:
a[行][列]
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
Console.Write(a[i][j] + "\t");
}
Console.WriteLine();
}
集合:
數組與集合的區別:
數組:同一類型,連續存儲。優勢:存儲效率高,檢索速度快。 缺點:連續的,不能擴展,不能刪除,不能插入
集合:同一類型,不連續存儲。
集合的操做:——ArrayList是集合中的一種
1、鏈表--每一個存儲值都會分配一個索引號,經過索引號可對每一個元素賦值或者取值
弱類型的集合:
ArrayList list = new ArrayList();
添加操做:list.Add(值);
插入操做:list.Insert(下標,值)
刪除操做:list.RemoveAt(下標)
清空集合:list.Clear()
得到集合中元素個數:int n = list.Count;
獲取某個值:object n = list[下標];
強類型的集合:
List<類型> 集合名 = new List<類型>();
添加操做:list.Add(值);
插入操做:list.Insert(下標,值)
刪除操做:list.RemoveAt(下標)
清空集合:list.Clear()
得到集合中元素個數:int型名 n = list.Count;
2、哈希表:--每一個元素都由兩部分組成,一部分叫key,一部分叫value
弱類型
Hashtable list = new Hashtable();
添加操做:list.Add("名","值");
刪除操做:list.Remove("名");
清空集合:list.Clear()
得到集合中元素個數:int n = list.Count;
獲取值: object n = list["名"];
強類型:
Dictionary<string, string> table = new Dictionary<string, string>();
添加操做:table.Add("名","值");
刪除操做:table.Remove("名");
清空集合:table.Clear()
得到集合中元素個數:int n = table.Count;
獲取值: object n = table["名"];
三。隊列:特色:先進先出
Queue<string> s = new Queue<string>();
進隊:s.Enqueue("我"); //進隊
出隊:sting str = s.Dequeue();
不能插隊,不能中間離隊。
4、棧:特色:先進後出。
Stack<string> s = new Stack<string>();//棧
進棧:s.Push("我");
出棧:string str = s.Pop();
不能中間插入,不能中間離開。
5、遞歸——本身調本身 —— 未來可能會用到,可是如今僅作了解。
int Add(int a)
{
int b = Add(a+1);
Console.WriteLine(b);
}
void 講故事()
{
Console.Write("從前。。。,老和尚說:");
講故事();
}
void 找子級文件夾(當前文件夾)
{
if(當前文件夾下沒有子文件夾)
{
return;
}
找子級文件夾(當前文件夾下的第一個子文件夾);
}
//猴子吃桃子。
static int TaoZi(int day) //接收天數,返回這一天的桃子數
{
if (day == 7)
{
return 1;
}
int c = (TaoZi(day+1) + 1) * 2;
return c;
}
//程序員與富翁:
static double Money(int day)
{
if (day == 1)
{
return 0.01;
}
double a = Money(day-1) * 2;
return a;
}
6、枚舉:——結構體。枚舉也是咱們本身定義的類型。
可使用它替代一些難以記憶的整數
枚舉和整數之間能夠互相轉換
語法:
[public] enum 枚舉名
{
值1;
值2;
值3;
......
}
[public]:訪問修飾符。公開的公共的,哪均可以訪問。
enum :關鍵字,聲明枚舉的關鍵字
枚舉名:要符合Pascal命名規範
使用:枚舉名.成員名(值名)
將枚舉聲明到命名空間的下面,類的外邊,表示這個命名空間下,全部的類均可以使用這個枚舉。
枚舉就是一個變量類型,例如 int double string decimal 只是枚舉命名、賦值、使用的方式跟那些普通的變量類型不同。