花了將近20天終于把第一季剩下的學完了,越到后面感覺好多不懂,都不知道如何著手寫程序,一定要多練多思考,雖然感覺很難,但是不能放棄.
整理下學習的筆記,現在想想過的還是很充實的,雖然學習很忙,但是課外時間全都用在自學C#上了,接下來還有很長的路要走,還得繼續努力才行.
break:
(1)用于switch-case判斷中,用于跳出switch.
(2)用于跳出當前循環.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace break_continue{ class PRogram { static void Main(string[] args) { bool flag = true; int age = 0; int sum = 0; for (int i = 0; i < 5; i++) { Console.WriteLine("請輸入{0}個人的年齡?",i+1); try { age = Convert.ToInt32(Console.ReadLine()); //sum += age; if (age < 0 || age > 100) { Console.WriteLine("你輸入的年齡非法."); flag = false; break; } sum += age; } catch { Console.WriteLine("你輸入的年齡不是數字,請重新運行程序輸入."); flag = false; break; } } if (flag == true) { Console.WriteLine("這五個人的平均年齡是{0}", sum / 5); } Console.ReadKey(); } }}View CodeContinue:
用于循環中,程序一旦執行到continue語句,立即結束本次循環(就是不再執行循環體中continue下面的語句了),直接進行下一次循環.
(do-while直接進行下一次循環條件的判斷,如果條件成立,)
三元表達式:
表達式1?表達式2:表達式3;
首先計算表達式1(求解成一個bool類型的值)
如果表達式1的值為true,則表達式2的值作為整個表達式的值,
如果表達式1為false,則表達式2作為整個表達式的值.
枚舉/常量/結構:
常量: const 類型 常量名=常量值
在定義時賦值,其他地方不允許改變其值.
枚舉:(定義枚舉時,不能是int類型)
定義一種枚舉類型,并且在定義這種類型時,指定這個類型的所有的值
語法: enum 類型名稱{值1,值2,…..值n}
(一般和類的定義在同一個級別,這樣在同一個命名空間下所有的類都可以使用這個枚舉)
枚舉的作用(1)限制用戶不能隨意賦值,只能在定義的枚舉中的值選擇
(2)不需要死記每一個值,只需要選擇相應的值即可.枚舉的類型的變量都可以強制轉換成一個int類型.
把字符串轉化成枚舉類型:
(自枚)(Enum.Parse(typeof(自枚),”待轉換的字符串”))
結構語法(也是一種類型): 訪問修飾符 struct 結構名
(為什么要用結構)?
{
定義結構成員
}
定義好結構后,可以直接聲明相應的變量

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 結構{ enum Gender//枚舉 定義了一個 Gender的類 { 男, 女 } public struct Person //定義了一個 結構名 為 Person的類 { public string name; public int age; public string sex; } class Program { static void Main(string[] args) { Gender sex; sex = Gender.男; switch (sex) { case Gender.男: Console.WriteLine("男性"); break; case Gender.女: Console.WriteLine("女性"); break; default: break; } Person firstPenson,secondPenson; firstPenson.name = "小蘭"; firstPenson.sex = "女"; firstPenson.age = 18; Console.WriteLine("{0}今年{1}歲,性別是{2}.",firstPenson.name,firstPenson.age,firstPenson.sex); secondPenson.name = "張三"; secondPenson.sex = "男"; secondPenson.age = 20; Console.WriteLine("{0}今年{1}歲,性別是{2}.", secondPenson.name, secondPenson.age, secondPenson.sex); Console.ReadKey(); } }}View Code數組:一次聲明多個相同類型的變量.這些變量在內存中是連續存儲的.(求平均值,最大值求和,排序 )
語法:
數據類型[] 數組名=new 數據類型[數組長度];
數組長度:申明幾個變量長度就是多少,
例如5個人的成績,則長度為5
int[] score=new int[5] s聲明了一個數組,里面包含5個
int類型的變量,數組名叫:score.
訪問數組:數組名[]=值 score[0]=10;
int類型數組一旦聲明,里面的每一個元素背初始化為0.
通過 數組名.Length 可以獲取數組長度

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數組{ class Program { static void Main(string[] args) { int max; int sum = 0; int[] score = new int[10]; //定義了一個int類型,長度為10的數組. for (int i = 0; i < score.Length; i++) { //對數組中的值賦值 Console.WriteLine("請輸入第{0}個學生的成績", i + 1); score[i] = Convert.ToInt32(Console.ReadLine()); // sum+=score[i]; } //求數組中的最大值. max = score[0];//先假定最大值為數組中的第一個值. for (int i = 1; i < score.Length; i++) { if (score[i] > max) { max = score[i]; } } Console.Clear(); //通過一個循環求數組的所有值的和 for (int i = 0; i < score.Length; i++) { sum += score[i]; } Console.WriteLine("{0}個人平均成績是{1}", score.Length, sum / score.Length); //通過一個循環顯示所有人的成績 for (int i = 0; i < score.Length; i++) { Console.WriteLine("第{0}個人的成績是{1}", i + 1, score[i]); } Console.WriteLine("數組中最高分為:{0}", max); Console.ReadKey(); } }}View CodeConsole.Clear(); 用于清屏.
方法(函數):與Main方法同等級(在同一個括號里)
[訪問修飾符][static] 返回值類型方法名([參數])
[ ]中的可寫可不寫.
{
方法體;
}
(1)方法一般要定義在類中.
(2)如果方法沒有返回值,我們就寫void.
(3)如果方法沒有參數,小括號()不能省略.
如果是靜態方法(由static修飾) 則使用 類名.方法名();
在類中調用本調的方法可以直接寫方法名();
public static void 方法名([參數]);
{
}

新聞熱點
疑難解答