20150907~20150928所學基礎知識整理,後續完善補充數組
數據類型dom
變量ide
運算符函數
時間3d
類、類型code
數組、冒泡排序blog
數組、迷宮操做排序
集合遞歸
數據類型:
變量:用於臨時存儲數據的
變量的定義:
數據類型 變量名;
數據類型 變量名=賦值;
數據類型 變量名1,變量名2,變量名3...;
//生成隨機數的類
Random r = new Random();
int shu = r.Next(100);
Console.WriteLine("您的名字打分數爲:"+shu);
變量的賦值:變量名=值;
數據類型轉換:
顯示轉換:直接在數據或變量前,加(類型)
數值類型跟數值類型之間。
隱式轉換:
類型.Parse(數據);
Convert.To類型(數據);
從字符串轉成值類型,必需要在數值類型的承受範圍以內。
運算符:
算數運算符: + - * / % ++ --
比較運算符:﹥ ﹤ ≧ ≦ == !=
邏輯運算符:&& ∣∣ !
優先級:
前++,前--
* / %
+ -
> < >= <= == !=
&& ∣∣
!
有括號的先算括號
語句:
條件語句:實現選擇分支
if (條件表達式)
{
...
}
循環語句:實現重複操做
跳轉語句:結束循環
異常語句:抓取錯誤
例題 一、輸入三個整數XYZ,將他們排序後以從小到大的順序打印出來。
例題 二、有一組函數:y = x(x < 1); y = 2x - 1(1<=x<10);y = 3x-11(x>10). 括號內是X的知足條件。 實現功能,隨意輸入一個X值,輸出Y的值。
例題 三、輸入整數a和b,若a2+b2大於100,則輸出a2+b2百位以上數字,不然輸出兩數之和。
題目:判斷某一天是這一年的第多少天
題目:判斷某一天是這一年的第多少天 static void Main(string[] args) { while (true)//循環 { Console.Write("輸入年:"); int year = int.Parse(Console.ReadLine()); Console.Write("輸入月:"); int month = int.Parse(Console.ReadLine()); Console.Write("輸入日:"); int day = int.Parse(Console.ReadLine()); bool isok = false;//用來記錄日期是否正確 bool isrun = false; if (year >= 1759 && year < 9999) { if (month >= 1 && month <= 12) { if (month == 2) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //判斷閏年能被400整除或者能被4整除同時不能被100整除 { if (day >= 1 && day <= 29) { isok = true; isrun = true; } else { Console.WriteLine("應在29天範圍以內"); } } else { if (day >= 1 && day <= 28) { isok = true; } else { Console.WriteLine("應在28天範圍以內"); } } } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day >= 1 && day <= 31) { isok = true; } else { Console.WriteLine("日期應在31天以內"); } } if (month == 4 || month == 6 || month == 9 || month == 11) { if (day >= 1 && day <= 30) { isok = true; } else { Console.WriteLine("日期應在30天以內"); } } } else { Console.WriteLine("月份不正確"); } } else { Console.WriteLine("年份不正確"); } int sumday = 0;//第多少天 if (isok)//日期正確,計算天數 { if (isrun)//是閏年 { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 29 + day; } if (month == 4) { sumday = 31 + 29 + 31 + day; } if (month == 5) { sumday = 31 + 29 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 29 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } else { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 28 + day; } if (month == 4) { sumday = 31 + 28 + 31 + day; } if (month == 5) { sumday = 31 + 28 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 28 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } Console.WriteLine("第{0}天", sumday); } else { Console.WriteLine("已經日期不正確,算不出來!"); } Console.ReadLine(); } }
練習題
==猜拳題目== //猜拳,剪刀-0 石頭-1 布-2 Random r = new Random();//生成隨機數 int diannao = r.Next(3);//生成0-3之間,不包括3的數 Console.Write("請出拳:剪刀-0 石頭-1 布-2 :"); int ren = int.Parse(Console.ReadLine());//控制檯錄入字符串,轉換爲int類型 Console.WriteLine("電腦出拳:{0}",diannao); if (diannao - ren == -1 || ren - diannao == -2)//全部人贏的狀況 { Console.WriteLine("恭喜你,你贏了"); } else if (diannao == ren) { Console.WriteLine("平局"); } else { Console.WriteLine("輸了"); } == 猜拳:三局兩勝=== int renying = 0; int dnying = 0; for (int i = 1; i <= 3; i++) { Random r = new Random();//生成隨機數 int diannao = r.Next(3);//生成0-3之間,不包括3的數 Console.Write("請出拳:剪刀-0 石頭-1 布-2 :"); int ren = int.Parse(Console.ReadLine());//控制檯錄入字符串,轉換爲int類型 Console.WriteLine("電腦出拳:{0}", diannao); if (diannao - ren == -1 || ren - diannao == -2)//全部人贏的狀況 { renying++; Console.WriteLine("恭喜你,你贏了"); } else if (diannao == ren) { Console.WriteLine("平局"); } else { dnying++; Console.WriteLine("輸了"); } } if (renying >= 2) { Console.WriteLine("三局兩勝人贏了"); } if (dnying >= 2) { Console.WriteLine("三局兩勝電腦贏了"); } ==累加求和== int sum = 0; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { sum = sum + i;=====èsum+ = i ; } Console.WriteLine("結果爲:"+sum); ==輸出偶數== Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { if (i % 2 == 0) { Console.WriteLine(i); } } ==與7相關的數== for (int i = 1; i <= 100; i++) { if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) { Console.WriteLine(i); } } ==求階乘== int sum = 1; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { sum = sum * i ; } Console.WriteLine("結果爲:" + sum); int sum = 0; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++)//從1開始遍歷 { int jiecheng = 1; //求i的階乘 for (int j = 1; j <= i; j++) { jiecheng = jiecheng*j; } sum = sum + jiecheng;//累加 } Console.WriteLine("結果爲:" + sum); ===籃球==== Console.Write("請輸入次數:"); int n = int.Parse(Console.ReadLine()); decimal height = 10M; for (int i = 1; i <= n; i++) { height = height * 3 / 4; } Console.WriteLine("高度爲:"+height); 窮舉 ======100塊錢,買2元一隻的圓珠筆,3塊錢一個的尺子,5元一個的鉛筆盒,每樣至少一個,正好花光,有多少種花法。====== Console.WriteLine("圓珠筆 尺子 鉛筆盒"); int count = 0; for (int i = 1; i <= 50; i++) { for (int j = 1; j <= 33; j++) { for (int k = 1; k <= 20; k++) { if (i * 2 + j * 3 + k * 5 == 100) { Console.WriteLine("圓珠筆:{0},尺子{1},鉛筆盒{2}",i,j,k); count++; } } } } Console.WriteLine("總個數爲:" + count); ====一張紙0.00007m,折多少次和珠峯同樣高==== decimal hou = 0.00007M; for (int i = 1; i > 0; i++) { hou = hou * 2; if (hou > 8848) { Console.WriteLine(i); break;//跳出循環 } } ====100之內質數==== for (int k = 1; k <= 100; k++) { int count = 0; for (int i = 1; i <= k; i++) { if (k % i == 0) { count++; } } if (count == 2) { Console.WriteLine(k); } } ====100之內質數和==== int sum = 0; for (int k = 1; k <= 100; k++) { int count = 0; for (int i = 1; i <= k; i++) { if (k % i == 0) { count++; } } if (count == 2) { sum += k; } } Console.WriteLine(sum); ======一對幼兔,1個月後長成小兔,再過一個月長成成兔而且生下一對幼兔,問,24個月後,有多少對兔子。====== Console.Write("請輸入月數:"); int m =int.Parse(Console.ReadLine()); int ct = 0 ;//成兔 int xt = 0 ;//小兔 int yt = 1 ;//幼兔 int zt = 1 ;//總兔 for (int i = 1; i <= m; i++) { if (i == 1) { ct = 0; xt = 0; yt = 1; } else { ct = xt + ct; xt = yt; yt = ct; } zt = ct + xt + yt; Console.WriteLine(i.ToString() + "個月後兔子總對數是:" + zt.ToString()); Console.ReadLine(); }
循環語句:
while循環: 初始條件; while(條件表達式) { 循環體; 狀態改變; } 例:10之內累加求和 int sum =0; int i = 1; while(i<=10) { sum+=i; i++; } do { } while();循環 初始條件; do { 循環體; 狀態改變; } while(條件表達式) foreach(in) xx數組集合的
跳轉語句:
Break 結束跳轉 Continue 結束本次,繼續下次循環 例: for (int i = 0;i<=10;i++) { Console.WriteLine(i); if (i==3) ________> 0 1 2 3 4 5 6 7 8 9 10 Break; } for (int i = 0;i<=10;i++) { if (i==3) ________> 0 1 2 4 5 6 7 8 9 10 Break; Console.WriteLine(i); } Random r = new Random(); int a, b, c; for (int i = 1;i>=1 ;i++ ) { a = r.Next(10); b = r.Next(10); c = r.Next(10); if(a != b && a != c && b != c) { Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); break; } } Console.ReadLine();
異常語句:
try { int a = int.Parse(Console.ReadLine()); } catch (Exception ex) { // throw ex;====>拋出異常 Console.WriteLine("出現錯誤了:"+ex.Message); } finally { //無論 }
時間:
//必須經過new初始化變量,後面括號實際是構造函數 //datetime實際是一個類 class DateTime dt = new DateTime(2015,9,14); Console.WriteLine(dt.ToString()); Console.ReadLine();
例:
int cuo = 0; Console.WriteLine("請輸入:"); string s = Console.ReadLine(); try { DateTime dt = DateTime.Parse(s); } catch (Exception ex) { Console.WriteLine("不是" ); cuo = 1; } if( cuo == 0) { Console.WriteLine("正確"); } Console.ReadLine();
類、類型:
class類型:用戶自定義類型、 String :處理字符串 s.Length;//返回字符串的長度 s.Trim();//去除空格 s.TrimStart();//去除前空格 s.TrimEnd();//去除後空格 s.ToLower();//轉換爲小寫 s.ToUpper();//轉換爲大寫 //截取字符串:字符串是有索引的,索引從0開始 // s.Substring(); //查找索引indexOf lastindexof replace split int id = js.IndexOf("ef",5);//查第一個匹配項的索引 |========>查找第二個索引 Console.WriteLine(id); int lid = js.LastIndexOf("ef");//查找最後一個索引 Console.WriteLine(lid); js.Replace("ef","123");//查找替換 string news = js.Replace("ef","123"); Console.WriteLine(js); Console.WriteLine(news); string s = "a|b|c|ab|cd|c|d"; string [] str = s.Split('|');//分割 foreach(string d in str) { Console.WriteLine(d); } Datetime:處理時間 Random:生成隨機數 Math:處理數字的 //Math:裏面有些處理數字的方法,靜態方法、 int i = Math.Abs(-5);//取絕對值 Console.WriteLine(i); double a = Math.Ceiling(1.1);//天花板 上限取整 Console.WriteLine(a); Math.Floor(1.9);//下限取整 Console.WriteLine(Math.PI);//圓周率 Math.Round(1.45);//四捨五入 Console.WriteLine(Math.Round(1.45)); Console.WriteLine(Math.Pow(2,3));//冪次方 Console.WriteLine(Math.Sqrt(16));//平方 //Random 隨機數 Random r = new Random(); int shu = r.Next(1,34);
隨機數例題:
Random r = new Random(); for (int i= 0;i<=100 ; i++) { int shu = r.Next(1001,9999); Console.WriteLine(shu ); Thread.Sleep(100); Console.Clear(); } Console.WriteLine("中獎號碼是:"); Console.ReadLine();
數組、冒泡排序 及相關習題彩票等
static void Main(string[] args) { while (true) { 數組:一組同類型的數據,數組是有長度的,數組是有索引的,索引從0開始 =====彩票===== int[] shuzu = new int[7];//定義了一個長度爲6的int類型的數組 Random r = new Random(); for (int i = 0; i < 6; i++)//循環生成六個數 { shuzu[i] = r.Next(1, 34);//生成一個數 bool isok = false; for (int j = 0; j < i; j++)//比較是否跟以前的書相等 { if (shuzu[j] == shuzu[i]) { isok = true; } } if (isok) { i--;//後退一步 continue; } } shuzu[6] = r.Next(1, 17); //輸入你的號碼 Console.Write("請輸入紅球6個,藍球1個,逗號隔開:"); string shuru = Console.ReadLine(); string[] ren = shuru.Split(','); //判斷中了幾個紅球 int count = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (int.Parse(ren[i]) == shuzu[j]) { count++; } } } //判斷藍球中沒中 bool islan = false; if (int.Parse(ren[6]) == shuzu[6]) { islan = true; } //輸出電腦隨機的中獎號碼 foreach (int a in shuzu) { Console.Write(a + " | "); } //判斷中幾等獎 if (count == 6 && islan) { Console.Write("一等獎"); } else if (count == 6 && !islan) { Console.Write("二等獎"); } else if (count == 5 && islan) { Console.Write("三等獎"); } else if ((count == 4 && islan) || (count == 5 && !islan)) { Console.Write("四等獎"); } else if ((count == 3 && islan) || (count == 4 && !islan)) { Console.Write("五等獎"); } else if ((count == 2 && islan) || (count == 1 && islan) || (count == 0 && islan)) { Console.Write("五塊錢"); } else { Console.Write("別再買了"); } Console.ReadLine(); } ====輸入10我的的分數,求最高分最低分平均分==== int[] fenshu = new int[10]; int max = 0; int min = 0; int sum = 0; for (int i = 0; i < 10; i++) { fenshu[i] = int.Parse(Console.ReadLine()); sum += fenshu[i]; if (i == 0) { max = fenshu[i]; min = fenshu[i]; } else { if (max < fenshu[i]) { max = fenshu[i]; } if (min > fenshu[i]) { min = fenshu[i]; } } } Console.WriteLine("最大值:"+max); Console.WriteLine("最小值:" + min); Console.WriteLine("平均分:" + sum/10); ====輸入全班同窗的年齡,按年齡從大到小排序===== Console.Write("請輸入人數:"); int n = int.Parse(Console.ReadLine()); int[] nianling = new int[n]; for (int i = 0; i < n; i++) { Console.Write("請輸入第{0}我的的年齡:",i+1); nianling[i] = int.Parse(Console.ReadLine()); } //開始排序 for (int j = 0; j < n - 1; j++) { for (int i = j+1; i < n; i++) { if (nianling[i] > nianling[j]) { //等量代換 int zhong = nianling[i]; nianling[i] = nianling[j]; nianling[j] = zhong; } } } foreach (int i in nianling) { Console.WriteLine(i); } Console.ReadLine(); }
數組,簡單的迷宮操做
數組:有固定長度的同種類型的一組變量,有索引,索引從0開始。 Int [ ]shuzu = new int[5]; shuzu [0] = 4; shuzu [1] = 6; 或直接賦值: int[]shuzu = new int[5]{2,4,5,7,9}; Console.Write(shuzu[3]); Console.Readline(); 這是一維數組, 二維數組是: int[,] erwei = new int[2,5]; =========>兩個長度爲5的一維數組 賦值: Int [ , ] erwei = new int[2,5]; { {1,2,3,4,5}, {2,12,5,7,9} }; Console.Write(erwei[1,1]); Console.Readline(); 而後練習了簡單的迷宮遊戲操做。 "\n" ======>換行 "\t" ======>空格
集合ArrayList:跟數組比,不限數量,不限類型
集合ArrayList:跟數組比,不限數量,不限類型 ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(7); arr.Add("fadf"); string s = "你好"; arr.Add(s); Console.WriteLine(arr[0]); int count = arr.Count; Console.WriteLine("元素個數爲:" + count); // arr.Clear();//清空集合 bool isok = arr.Contains(7); Console.WriteLine(isok); arr.Insert(2,"zhangsan");往集合裏插入元素,往指定索引上插入數據 arr.Remove("zhangsan");移除第一個匹配項 /arr.RemoveAt(2);移除指定索引上的數據 int index = arr.IndexOf("fadf");查找匹配項,返回匹配項的索引 Console.WriteLine("索引爲:"+index); foreach (object o in arr) { Console.WriteLine(o); } //輸入十我的的分數,放入集合當中 ArrayList list = new ArrayList(); for (int i = 0; i < 5; i++) { string s = Console.ReadLine(); list.Add(s); } list.Sort();//排序 list.Reverse();//翻轉集合 foreach (string s in list) { Console.WriteLine(s); } 特殊集合stack、 queue、 hashtable //棧橋 Stack s = new Stack(); s.Push(3);//推入集合 s.Push(5); s.Push(7); foreach(int a in s) { Console.WriteLine(a); } int count = s.Count; int qu = int.Parse(s.Pop().ToString());//彈出最後一個元素 Console.WriteLine(qu); s.Clear();//清空集合 //隊列 Queue q = new Queue(); q.Enqueue(3); q.Enqueue(5); q.Enqueue(7); int chu = int.Parse(q.Dequeue().ToString()); foreach (int a in q) { Console.WriteLine(a); } Hashtable hs = new Hashtable(); hs.Add(3, "張三"); hs.Add("4", "李四"); //foreach (int i in hs.Keys) //{ // Console.WriteLine(i); //} foreach (string s in hs.Values) { Console.WriteLine(s); } int count = hs.Count;//元素個數 Console.WriteLine(hs["4"]); Console.ReadLine();
結構體及題目
結構體:用戶自定義數據類型,實際就是變量組,能夠一次存多個不一樣變量 結構體定義在main函數的外面 Struck 結構體名 { // 元素一 // 元素二 } 題目:定義一個學生的結構體,學號,姓名,身高,輸入學生信息,按身高排序輸出
枚舉、函數:
枚舉:常量組
常量:變量前面加const
枚舉的索引:
函數:
修飾符 返回值 函數名(形參1,形參2,……形參N)
{
函數體
}
當一個流程徹底封閉,完整的時候,而且須要屢次使用的時候纔去寫函數。
寫在main函數外面,class裏面
無返回值無參數
有返回值,無參數的
有返回值,有參數的
有多個返回值
猜拳、輸出偶數:
函數返回多個值:一元二次方程
練習題目:一元二次方程
遞歸:賣羊,沒過一個村莊賣掉總數的二分之一零一隻羊,過了七個村莊後還剩兩隻,問最初趕了多少隻羊
---恢復內容結束---
數據類型:
變量:用於臨時存儲數據的
變量的定義:
數據類型 變量名;
數據類型 變量名=賦值;
數據類型 變量名1,變量名2,變量名3...;
//生成隨機數的類
Random r = new Random();
int shu = r.Next(100);
Console.WriteLine("您的名字打分數爲:"+shu);
變量的賦值:變量名=值;
數據類型轉換:
顯示轉換:直接在數據或變量前,加(類型)
數值類型跟數值類型之間。
隱式轉換:
類型.Parse(數據);
Convert.To類型(數據);
從字符串轉成值類型,必需要在數值類型的承受範圍以內。
運算符:
算數運算符: + - * / % ++ --
比較運算符:﹥ ﹤ ≧ ≦ == !=
邏輯運算符:&& ∣∣ !
優先級:
前++,前--
* / %
+ -
> < >= <= == !=
&& ∣∣
!
有括號的先算括號
語句:
條件語句:實現選擇分支
if (條件表達式)
{
...
}
循環語句:實現重複操做
跳轉語句:結束循環
異常語句:抓取錯誤
例題 一、輸入三個整數XYZ,將他們排序後以從小到大的順序打印出來。
例題 二、有一組函數:y = x(x < 1); y = 2x - 1(1<=x<10);y = 3x-11(x>10). 括號內是X的知足條件。 實現功能,隨意輸入一個X值,輸出Y的值。
例題 三、輸入整數a和b,若a2+b2大於100,則輸出a2+b2百位以上數字,不然輸出兩數之和。
題目:判斷某一天是這一年的第多少天
題目:判斷某一天是這一年的第多少天 static void Main(string[] args) { while (true)//循環 { Console.Write("輸入年:"); int year = int.Parse(Console.ReadLine()); Console.Write("輸入月:"); int month = int.Parse(Console.ReadLine()); Console.Write("輸入日:"); int day = int.Parse(Console.ReadLine()); bool isok = false;//用來記錄日期是否正確 bool isrun = false; if (year >= 1759 && year < 9999) { if (month >= 1 && month <= 12) { if (month == 2) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //判斷閏年能被400整除或者能被4整除同時不能被100整除 { if (day >= 1 && day <= 29) { isok = true; isrun = true; } else { Console.WriteLine("應在29天範圍以內"); } } else { if (day >= 1 && day <= 28) { isok = true; } else { Console.WriteLine("應在28天範圍以內"); } } } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day >= 1 && day <= 31) { isok = true; } else { Console.WriteLine("日期應在31天以內"); } } if (month == 4 || month == 6 || month == 9 || month == 11) { if (day >= 1 && day <= 30) { isok = true; } else { Console.WriteLine("日期應在30天以內"); } } } else { Console.WriteLine("月份不正確"); } } else { Console.WriteLine("年份不正確"); } int sumday = 0;//第多少天 if (isok)//日期正確,計算天數 { if (isrun)//是閏年 { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 29 + day; } if (month == 4) { sumday = 31 + 29 + 31 + day; } if (month == 5) { sumday = 31 + 29 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 29 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } else { if (month == 1) { sumday = day; } if (month == 2) { sumday = 31 + day; } if (month == 3) { sumday = 31 + 28 + day; } if (month == 4) { sumday = 31 + 28 + 31 + day; } if (month == 5) { sumday = 31 + 28 + 31 + 30 + day; } if (month == 6) { sumday = 31 + 28 + 31 + 30 + 31 + day; } if (month == 7) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + day; } if (month == 8) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; } if (month == 9) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; } if (month == 10) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; } if (month == 11) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; } if (month == 12) { sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; } } Console.WriteLine("第{0}天", sumday); } else { Console.WriteLine("已經日期不正確,算不出來!"); } Console.ReadLine(); } }
練習題
==猜拳題目== //猜拳,剪刀-0 石頭-1 布-2 Random r = new Random();//生成隨機數 int diannao = r.Next(3);//生成0-3之間,不包括3的數 Console.Write("請出拳:剪刀-0 石頭-1 布-2 :"); int ren = int.Parse(Console.ReadLine());//控制檯錄入字符串,轉換爲int類型 Console.WriteLine("電腦出拳:{0}",diannao); if (diannao - ren == -1 || ren - diannao == -2)//全部人贏的狀況 { Console.WriteLine("恭喜你,你贏了"); } else if (diannao == ren) { Console.WriteLine("平局"); } else { Console.WriteLine("輸了"); } == 猜拳:三局兩勝=== int renying = 0; int dnying = 0; for (int i = 1; i <= 3; i++) { Random r = new Random();//生成隨機數 int diannao = r.Next(3);//生成0-3之間,不包括3的數 Console.Write("請出拳:剪刀-0 石頭-1 布-2 :"); int ren = int.Parse(Console.ReadLine());//控制檯錄入字符串,轉換爲int類型 Console.WriteLine("電腦出拳:{0}", diannao); if (diannao - ren == -1 || ren - diannao == -2)//全部人贏的狀況 { renying++; Console.WriteLine("恭喜你,你贏了"); } else if (diannao == ren) { Console.WriteLine("平局"); } else { dnying++; Console.WriteLine("輸了"); } } if (renying >= 2) { Console.WriteLine("三局兩勝人贏了"); } if (dnying >= 2) { Console.WriteLine("三局兩勝電腦贏了"); } ==累加求和== int sum = 0; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { sum = sum + i;=====èsum+ = i ; } Console.WriteLine("結果爲:"+sum); ==輸出偶數== Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { if (i % 2 == 0) { Console.WriteLine(i); } } ==與7相關的數== for (int i = 1; i <= 100; i++) { if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) { Console.WriteLine(i); } } ==求階乘== int sum = 1; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++) { sum = sum * i ; } Console.WriteLine("結果爲:" + sum); int sum = 0; Console.Write("請輸入數字:"); int shu = int.Parse(Console.ReadLine()); for (int i = 1; i <= shu; i++)//從1開始遍歷 { int jiecheng = 1; //求i的階乘 for (int j = 1; j <= i; j++) { jiecheng = jiecheng*j; } sum = sum + jiecheng;//累加 } Console.WriteLine("結果爲:" + sum); ===籃球==== Console.Write("請輸入次數:"); int n = int.Parse(Console.ReadLine()); decimal height = 10M; for (int i = 1; i <= n; i++) { height = height * 3 / 4; } Console.WriteLine("高度爲:"+height); 窮舉 ======100塊錢,買2元一隻的圓珠筆,3塊錢一個的尺子,5元一個的鉛筆盒,每樣至少一個,正好花光,有多少種花法。====== Console.WriteLine("圓珠筆 尺子 鉛筆盒"); int count = 0; for (int i = 1; i <= 50; i++) { for (int j = 1; j <= 33; j++) { for (int k = 1; k <= 20; k++) { if (i * 2 + j * 3 + k * 5 == 100) { Console.WriteLine("圓珠筆:{0},尺子{1},鉛筆盒{2}",i,j,k); count++; } } } } Console.WriteLine("總個數爲:" + count); ====一張紙0.00007m,折多少次和珠峯同樣高==== decimal hou = 0.00007M; for (int i = 1; i > 0; i++) { hou = hou * 2; if (hou > 8848) { Console.WriteLine(i); break;//跳出循環 } } ====100之內質數==== for (int k = 1; k <= 100; k++) { int count = 0; for (int i = 1; i <= k; i++) { if (k % i == 0) { count++; } } if (count == 2) { Console.WriteLine(k); } } ====100之內質數和==== int sum = 0; for (int k = 1; k <= 100; k++) { int count = 0; for (int i = 1; i <= k; i++) { if (k % i == 0) { count++; } } if (count == 2) { sum += k; } } Console.WriteLine(sum); ======一對幼兔,1個月後長成小兔,再過一個月長成成兔而且生下一對幼兔,問,24個月後,有多少對兔子。====== Console.Write("請輸入月數:"); int m =int.Parse(Console.ReadLine()); int ct = 0 ;//成兔 int xt = 0 ;//小兔 int yt = 1 ;//幼兔 int zt = 1 ;//總兔 for (int i = 1; i <= m; i++) { if (i == 1) { ct = 0; xt = 0; yt = 1; } else { ct = xt + ct; xt = yt; yt = ct; } zt = ct + xt + yt; Console.WriteLine(i.ToString() + "個月後兔子總對數是:" + zt.ToString()); Console.ReadLine(); }
循環語句:
while循環: 初始條件; while(條件表達式) { 循環體; 狀態改變; } 例:10之內累加求和 int sum =0; int i = 1; while(i<=10) { sum+=i; i++; } do { } while();循環 初始條件; do { 循環體; 狀態改變; } while(條件表達式) foreach(in) xx數組集合的
跳轉語句:
Break 結束跳轉 Continue 結束本次,繼續下次循環 例: for (int i = 0;i<=10;i++) { Console.WriteLine(i); if (i==3) ________> 0 1 2 3 4 5 6 7 8 9 10 Break; } for (int i = 0;i<=10;i++) { if (i==3) ________> 0 1 2 4 5 6 7 8 9 10 Break; Console.WriteLine(i); } Random r = new Random(); int a, b, c; for (int i = 1;i>=1 ;i++ ) { a = r.Next(10); b = r.Next(10); c = r.Next(10); if(a != b && a != c && b != c) { Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); break; } } Console.ReadLine();
異常語句:
try { int a = int.Parse(Console.ReadLine()); } catch (Exception ex) { // throw ex;====>拋出異常 Console.WriteLine("出現錯誤了:"+ex.Message); } finally { //無論 }
時間:
//必須經過new初始化變量,後面括號實際是構造函數 //datetime實際是一個類 class DateTime dt = new DateTime(2015,9,14); Console.WriteLine(dt.ToString()); Console.ReadLine();
例:
int cuo = 0; Console.WriteLine("請輸入:"); string s = Console.ReadLine(); try { DateTime dt = DateTime.Parse(s); } catch (Exception ex) { Console.WriteLine("不是" ); cuo = 1; } if( cuo == 0) { Console.WriteLine("正確"); } Console.ReadLine();
類、類型:
class類型:用戶自定義類型、 String :處理字符串 s.Length;//返回字符串的長度 s.Trim();//去除空格 s.TrimStart();//去除前空格 s.TrimEnd();//去除後空格 s.ToLower();//轉換爲小寫 s.ToUpper();//轉換爲大寫 //截取字符串:字符串是有索引的,索引從0開始 // s.Substring(); //查找索引indexOf lastindexof replace split int id = js.IndexOf("ef",5);//查第一個匹配項的索引 |========>查找第二個索引 Console.WriteLine(id); int lid = js.LastIndexOf("ef");//查找最後一個索引 Console.WriteLine(lid); js.Replace("ef","123");//查找替換 string news = js.Replace("ef","123"); Console.WriteLine(js); Console.WriteLine(news); string s = "a|b|c|ab|cd|c|d"; string [] str = s.Split('|');//分割 foreach(string d in str) { Console.WriteLine(d); } Datetime:處理時間 Random:生成隨機數 Math:處理數字的 //Math:裏面有些處理數字的方法,靜態方法、 int i = Math.Abs(-5);//取絕對值 Console.WriteLine(i); double a = Math.Ceiling(1.1);//天花板 上限取整 Console.WriteLine(a); Math.Floor(1.9);//下限取整 Console.WriteLine(Math.PI);//圓周率 Math.Round(1.45);//四捨五入 Console.WriteLine(Math.Round(1.45)); Console.WriteLine(Math.Pow(2,3));//冪次方 Console.WriteLine(Math.Sqrt(16));//平方 //Random 隨機數 Random r = new Random(); int shu = r.Next(1,34);
隨機數例題:
Random r = new Random(); for (int i= 0;i<=100 ; i++) { int shu = r.Next(1001,9999); Console.WriteLine(shu ); Thread.Sleep(100); Console.Clear(); } Console.WriteLine("中獎號碼是:"); Console.ReadLine();
數組、冒泡排序 及相關習題彩票等
static void Main(string[] args) { while (true) { 數組:一組同類型的數據,數組是有長度的,數組是有索引的,索引從0開始 =====彩票===== int[] shuzu = new int[7];//定義了一個長度爲6的int類型的數組 Random r = new Random(); for (int i = 0; i < 6; i++)//循環生成六個數 { shuzu[i] = r.Next(1, 34);//生成一個數 bool isok = false; for (int j = 0; j < i; j++)//比較是否跟以前的書相等 { if (shuzu[j] == shuzu[i]) { isok = true; } } if (isok) { i--;//後退一步 continue; } } shuzu[6] = r.Next(1, 17); //輸入你的號碼 Console.Write("請輸入紅球6個,藍球1個,逗號隔開:"); string shuru = Console.ReadLine(); string[] ren = shuru.Split(','); //判斷中了幾個紅球 int count = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (int.Parse(ren[i]) == shuzu[j]) { count++; } } } //判斷藍球中沒中 bool islan = false; if (int.Parse(ren[6]) == shuzu[6]) { islan = true; } //輸出電腦隨機的中獎號碼 foreach (int a in shuzu) { Console.Write(a + " | "); } //判斷中幾等獎 if (count == 6 && islan) { Console.Write("一等獎"); } else if (count == 6 && !islan) { Console.Write("二等獎"); } else if (count == 5 && islan) { Console.Write("三等獎"); } else if ((count == 4 && islan) || (count == 5 && !islan)) { Console.Write("四等獎"); } else if ((count == 3 && islan) || (count == 4 && !islan)) { Console.Write("五等獎"); } else if ((count == 2 && islan) || (count == 1 && islan) || (count == 0 && islan)) { Console.Write("五塊錢"); } else { Console.Write("別再買了"); } Console.ReadLine(); } ====輸入10我的的分數,求最高分最低分平均分==== int[] fenshu = new int[10]; int max = 0; int min = 0; int sum = 0; for (int i = 0; i < 10; i++) { fenshu[i] = int.Parse(Console.ReadLine()); sum += fenshu[i]; if (i == 0) { max = fenshu[i]; min = fenshu[i]; } else { if (max < fenshu[i]) { max = fenshu[i]; } if (min > fenshu[i]) { min = fenshu[i]; } } } Console.WriteLine("最大值:"+max); Console.WriteLine("最小值:" + min); Console.WriteLine("平均分:" + sum/10); ====輸入全班同窗的年齡,按年齡從大到小排序===== Console.Write("請輸入人數:"); int n = int.Parse(Console.ReadLine()); int[] nianling = new int[n]; for (int i = 0; i < n; i++) { Console.Write("請輸入第{0}我的的年齡:",i+1); nianling[i] = int.Parse(Console.ReadLine()); } //開始排序 for (int j = 0; j < n - 1; j++) { for (int i = j+1; i < n; i++) { if (nianling[i] > nianling[j]) { //等量代換 int zhong = nianling[i]; nianling[i] = nianling[j]; nianling[j] = zhong; } } } foreach (int i in nianling) { Console.WriteLine(i); } Console.ReadLine(); }
數組,簡單的迷宮操做
數組:有固定長度的同種類型的一組變量,有索引,索引從0開始。 Int [ ]shuzu = new int[5]; shuzu [0] = 4; shuzu [1] = 6; 或直接賦值: int[]shuzu = new int[5]{2,4,5,7,9}; Console.Write(shuzu[3]); Console.Readline(); 這是一維數組, 二維數組是: int[,] erwei = new int[2,5]; =========>兩個長度爲5的一維數組 賦值: Int [ , ] erwei = new int[2,5]; { {1,2,3,4,5}, {2,12,5,7,9} }; Console.Write(erwei[1,1]); Console.Readline(); 而後練習了簡單的迷宮遊戲操做。 "\n" ======>換行 "\t" ======>空格
集合ArrayList:跟數組比,不限數量,不限類型
集合ArrayList:跟數組比,不限數量,不限類型 ArrayList arr = new ArrayList(); arr.Add(5); arr.Add(7); arr.Add("fadf"); string s = "你好"; arr.Add(s); Console.WriteLine(arr[0]); int count = arr.Count; Console.WriteLine("元素個數爲:" + count); // arr.Clear();//清空集合 bool isok = arr.Contains(7); Console.WriteLine(isok); arr.Insert(2,"zhangsan");往集合裏插入元素,往指定索引上插入數據 arr.Remove("zhangsan");移除第一個匹配項 /arr.RemoveAt(2);移除指定索引上的數據 int index = arr.IndexOf("fadf");查找匹配項,返回匹配項的索引 Console.WriteLine("索引爲:"+index); foreach (object o in arr) { Console.WriteLine(o); } //輸入十我的的分數,放入集合當中 ArrayList list = new ArrayList(); for (int i = 0; i < 5; i++) { string s = Console.ReadLine(); list.Add(s); } list.Sort();//排序 list.Reverse();//翻轉集合 foreach (string s in list) { Console.WriteLine(s); } 特殊集合stack、 queue、 hashtable //棧橋 Stack s = new Stack(); s.Push(3);//推入集合 s.Push(5); s.Push(7); foreach(int a in s) { Console.WriteLine(a); } int count = s.Count; int qu = int.Parse(s.Pop().ToString());//彈出最後一個元素 Console.WriteLine(qu); s.Clear();//清空集合 //隊列 Queue q = new Queue(); q.Enqueue(3); q.Enqueue(5); q.Enqueue(7); int chu = int.Parse(q.Dequeue().ToString()); foreach (int a in q) { Console.WriteLine(a); } Hashtable hs = new Hashtable(); hs.Add(3, "張三"); hs.Add("4", "李四"); //foreach (int i in hs.Keys) //{ // Console.WriteLine(i); //} foreach (string s in hs.Values) { Console.WriteLine(s); } int count = hs.Count;//元素個數 Console.WriteLine(hs["4"]); Console.ReadLine();
結構體及題目
結構體:用戶自定義數據類型,實際就是變量組,能夠一次存多個不一樣變量 結構體定義在main函數的外面 Struck 結構體名 { // 元素一 // 元素二 } 題目:定義一個學生的結構體,學號,姓名,身高,輸入學生信息,按身高排序輸出
枚舉、函數:
枚舉:常量組
常量:變量前面加const
枚舉的索引:
函數:
修飾符 返回值 函數名(形參1,形參2,……形參N)
{
函數體
}
當一個流程徹底封閉,完整的時候,而且須要屢次使用的時候纔去寫函數。
寫在main函數外面,class裏面
無返回值無參數
有返回值,無參數的
有返回值,有參數的
有多個返回值
猜拳、輸出偶數:
函數返回多個值:一元二次方程
練習題目:一元二次方程
遞歸:賣羊,沒過一個村莊賣掉總數的二分之一零一隻羊,過了七個村莊後還剩兩隻,問最初趕了多少隻羊