例題目錄:
1. 長方形形式的九九乘法表
2. 下三角形式的九九乘法表
3.水仙花數(shù)
4.輸入數(shù)的正逆之和
5. 閏年疹启、月份的判斷
6.比較大小
7.冒泡排序
8.數(shù)組元素的調(diào)換
9.數(shù)組元素的輸入輸出
10.面向?qū)ο?/p>
? ? ? ? ? ? 以下代碼均是使用控制臺進(jìn)行編譯的:
? ? ? ? ? ? a. Win+r打開命令行窗口
? ? ? ? ? ? b. 輸入devenv打開Visual Studio
? ? ? ? ? ? c.新建C#控制臺項(xiàng)目文件,編寫代碼
1. 長方形形式的九九乘法表
? ? ? ? 通過觀察外部循環(huán)加1,內(nèi)部循環(huán)從1蛮艰,逐步加1到9怠李;所以會用到兩次for循環(huán)全度,程序如下:
? ? for (int i = 1; i <= 9; i++)? ? ? ? ? ? ? ? ? ? //外部循環(huán) 术唬,從1開始到9?
? ? {
? ? ? ? ? ? for (int j = 1; j <= 9; j++)? ? ? ? ? ? //外部循環(huán)加1(i++)域醇,內(nèi)部循環(huán)從1到9
? ? ? ? ? ? {
? ? ? ? ? ? Console.Write("{0}*{1}={2}\t", i, j, i * j);? ? ? ? ? ? // \t是轉(zhuǎn)義字符,相當(dāng)于執(zhí)行一次Tab鍵空四個(gè)格
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine();? ? ? ? ? //內(nèi)部循環(huán)9次弧械,執(zhí)行一次Console.WriteLine();即換行
? ? }
? ? ? ? 其中 Console.Write();與Console.WriteLine();區(qū)別是前者連續(xù)輸出八酒,后者換行輸出。
? ? ? ? 輸入完代碼后刃唐,在Visual Studio的菜單欄中點(diǎn)擊生成-->生成解決方案羞迷,或者F6快捷鍵編譯代碼,在Visual Studio的菜單欄中點(diǎn)擊調(diào)試-->開始調(diào)試画饥,或者F5執(zhí)行代碼衔瓮。
2. 下三角形式的九九乘法表
? ? ? ? 九九乘法表的輸出形式分為長方形形式和下三角形式,通過觀察發(fā)現(xiàn)當(dāng)內(nèi)部循環(huán)數(shù)與外部循環(huán)數(shù)不同時(shí)抖甘,執(zhí)行連續(xù)空格輸出热鞍,當(dāng)內(nèi)部循環(huán)數(shù)與外部循環(huán)數(shù)相同時(shí),執(zhí)行換行輸出衔彻,所以要用到 if 判斷語句判斷內(nèi)部循環(huán)數(shù)與外部循環(huán)數(shù)是否相等薇宠,程序如下:
? for (int i = 1; i <= 9; i++)? ? ? ? //外部循環(huán)從 1 到 9
? ? {
? ? ? ? for (int j = 1; j <= i; j++)? ? ? ? //內(nèi)部循環(huán)從 1 到 i,到等于i
? ? ? ? {
? ? ? ? ? ? if (i == j)? ? ? ? ? ? ? ? //判斷當(dāng)內(nèi)部循環(huán)與外部循環(huán)的值相等的情況下艰额,要執(zhí)行的代碼
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}*{1}={2}", i, j, i * j);? ? ? ? //Console.WriteLine();換行輸出澄港,{0},{1},{2}為C#中的占位符。相當(dāng)于C語言中的%d,%c,%f 等柄沮。
? ? ? ? ? ? }
? ? ? ? ? ? else? ? ? ? ? ? // if 語句不成立的即:當(dāng)內(nèi)部循環(huán)與外部循環(huán)的值不等的情況下
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("{0}*{1}={2}\t", i, j, i * j);? ? ? ? //Console.Write();不換行輸出
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? ? ? 輸入完代碼后回梧,在Visual Studio的菜單欄中點(diǎn)擊生成-->生成解決方案,或者F6快捷鍵編譯代碼祖搓,在Visual Studio的菜單欄中點(diǎn)擊調(diào)試-->開始調(diào)試漂辐,或者F5執(zhí)行代碼。
3.水仙花數(shù)
? ? 水仙花數(shù)是指100到999之間某個(gè)數(shù):個(gè)位的立方 + 十位的立方 + 百位的立方 = 某個(gè)數(shù)( 區(qū)間為[100棕硫,999] )
求[100髓涯,999]之前的水仙花數(shù)的程序如下:
? ? int a = 1;
? ? int b = 0;
? ? int c = 0;
? ? for (int i = 100; i <=999; i++)
? ? {
? ? ? ? a = i%10;? ? ? ? ? ? ? //取出[100,999]之間某個(gè)數(shù)的個(gè)位
? ? ? ? b = i/ 100;? ? ? ? ? ? ? //取出[100,999]之間某個(gè)數(shù)的百位
? ? ? ? c = (i - a? - b * 100)/10;? ? ? ? ? ? ? //取出[100,999]之間某個(gè)數(shù)的十位
? ? ? ? if (a*a*a? + b * b * b + c*c*c == i)? ? ? ? ? ? //判斷是否滿足水仙花數(shù)的特征,當(dāng)為true是執(zhí)行if判斷條件內(nèi)的語句
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("{0}是水仙花數(shù)", i);
? ? ? ? }
? ? }
4.輸入數(shù)的正逆之和
? ? Console.WriteLine("請輸入一個(gè)正整數(shù)");? ? ? ? ? ? //在控制臺上打印內(nèi)容哈扮,Console為一個(gè)類纬纪,調(diào)用WriteLine方法
? ? try? ? ? ? ? ? ? ? ? ? ? ? //防止用戶輸入的不是提示的內(nèi)容
? ? {
? ? ? ? ? ? int number = Convert.ToInt32(Console.ReadLine());
? ? ? ? ? ? int a = number;
? ? ? ? ? ? for (int i = 0; i <= a; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}+{1}={2}", i, number--, number + 1 + i);
? ? ? ? ? ? }
? ? }
? ? catch? ? ? ? ? ? ? ? //當(dāng)try內(nèi)的代碼執(zhí)行出現(xiàn)錯(cuò)誤時(shí),執(zhí)行該代碼
? ? {
? ? ? ? ? ? Console.WriteLine("請輸入正整數(shù)");
? ? }
當(dāng)提示用戶輸入:請輸入一個(gè)數(shù)字滑肉,此處輸入的值為 6
5. 閏年包各、月份的判斷
? ? ? ? 要求用戶輸入一個(gè)年份和月份:判斷輸入的年份是否是閏年及該年的這個(gè)月份有多少天
? ? int year = 0;
? ? int month = 0;
? ? int day = 28;
? ? try
? ? {
? ? ? ? Console.WriteLine("請輸入年份");
? ? ? ? year = Convert.ToInt32(Console.ReadLine());? ? ? ? //C#默認(rèn)接收到用戶的類型為string類型
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請輸入月份");
? ? ? ? ? ? month = Convert.ToInt32(Console.ReadLine());? ? ? ? //接收用戶的輸入并把輸入的字符串類型轉(zhuǎn)化為整型
? ? ? ? ? ? if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)? ? ? ? ? ? //判斷是否是閏年
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0}年是閏年", year);
? ? ? ? ? ? ? ? if (month == 2)? ? ? ? ? ? ? ? //閏年的2月是一個(gè)特殊的月份,有29天
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? day = 29;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? switch (month)? ? ? ? ? ? //判斷月份有多少天
? ? ? ? ? ? {
//在switch--case結(jié)構(gòu)中靶庙,當(dāng)case含有相同的代碼時(shí)问畅,只在最后一個(gè)case下寫,其余的可以省略
? ? ? ? ? ? ? ? case 1:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? case 8:
? ? ? ? ? ? ? ? case 10:
? ? ? ? ? ? ? ? case 12:
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}年的第{1}個(gè)月有31天", year, month);? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? case 9:
? ? ? ? ? ? ? ? case 11:
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}年的第{1}月有30天", year, month);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:? ? ? ? ? ? ? ? //以上情況都不成立時(shí)執(zhí)行
? ? ? ? ? ? ? ? ? ? if (month <= 12 && month >= 1)? ? ? ? ?
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}年的第二個(gè)月有{1}", year, day);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? // //防止用戶輸入的月份不在[1,12]之間
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("請輸入正確的月份!程序退出;つ贰7恕!");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? //default 的結(jié)尾
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? catch? ? ? ? ? ? ? ? //當(dāng)輸入的月份格式不正確時(shí)執(zhí)行卵皂;? ? 2a, 8_ 等都為不正確的月份格式
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請輸入正確的月份!程序退出V让!灯变!");
? ? ? ? }
? ? }
? ? catch? ? ? ? ? ? ? ? //當(dāng)輸入的年份格式不正確時(shí)執(zhí)行殴玛;? ? 2022a,1998_ 等都為不正確的年份格式
? ? {
? ? ? ? Console.WriteLine("請正確輸入年份!程序退出L砘觥9鏊凇!");
? ? }
? ? Console.ReadKey();? ? ? ? ? ? ? ? //暫停的作用刃泌,程序運(yùn)行到這里暫停一下
當(dāng)輸入的年份是2000凡壤,月份為6時(shí),程序執(zhí)行結(jié)果如圖5所示:
當(dāng)輸入的年份是2022,月份為2時(shí)沐寺,程序執(zhí)行結(jié)果如圖6所示:
6.比較大小
? ? ? ? 比較三個(gè)數(shù)的大小并按從大到小輸出
? ? Console.WriteLine("請輸入第一個(gè)數(shù)字");? ? ? ? //提示用戶輸入一個(gè)數(shù)字
? ? int a = Convert.ToInt32(Console.ReadLine());? ? ? ? ? ? //接收用戶的輸入并把輸入的字符串類型轉(zhuǎn)化為整型
? ? Console.WriteLine("請輸入第二個(gè)數(shù)字");
? ? int b = Convert.ToInt32(Console.ReadLine());
? ? Console.WriteLine("請輸入第三個(gè)數(shù)字");
? ? int c = Convert.ToInt32(Console.ReadLine());? ? ? ? //定義三個(gè)整型變量來接收用戶輸入的三個(gè)整數(shù)
? ? if (a > b)? ? ? ? ? //a>b
? ? {
? ? ? ? ? ? if (b > c)? ? ? ? ? ? ? //b>c
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0},{1},{2}", a, b, c);
? ? ? ? ? ? }
? ? ? ? ? ? else if (c > a)? ? ? ? ? ? //b<c
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0},{1},{2}", c, a, b);
? ? ? ? ? ? }
? ? }
? ? else if (c > b)? ? ? ? ? ? ? ? //b>a
? ? {
? ? ? ? Console.WriteLine("{0},{1},{2}", c, b, a);
? ? }
? ? else if (c > a)? ? ? ? ? ? ? ? //b>c>a
? ? {
? ? ? ? Console.WriteLine("{0},{1},{2}", b, c, a);
? ? }
? ? else? ? ? ? ? ? ? ? //a>c且b>a
? ? {
? ? ? ? Console.WriteLine("{0},{1},{2}", b, c, a);
? ? }
當(dāng)提示用戶輸入時(shí):輸入的值分別是 8 , 5 , 1 時(shí),程序的執(zhí)行結(jié)果如圖所示:
?
? 7.冒泡排序
? ? ? ? for循環(huán)的嵌套混坞,將輸入的數(shù)組按升序排列狐援。
? ? int[] nums = { 0, 9, 8, 5, 6, 5, 4, 3, 2, 1, 0 };
? ? for (int i = 0; i < nums.Length; i++)? ? ? ? ? ? ? ? ? ? ? ? //nums.Length調(diào)用Length方法,計(jì)算數(shù)組nums[]的長度
? ? {
? ? ? ? ? for (int j = i + 1; j < nums.Length; j++)? ? ? ? ? ? //nums.Length調(diào)用Length方法究孕,計(jì)算數(shù)組nums[]的長度
? ? ? ? ? {
? ? ? ? ? ? ? ? if (nums[i] > nums[j])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? nums[i] = nums[i] - nums[j];? ? ? ? ? ? //不使用第三方變量啥酱,交換兩個(gè)數(shù)的值
? ? ? ? ? ? ? ? ? ? nums[j] = nums[j] +nums[i];
? ? ? ? ? ? ? ? ? ? nums[i] = nums[j] - nums[i];
? ? ? ? ? ? ? ? }
? ? ? ? ? }
? ? }
? ? for (int i = 0; i < nums.Length; i++)? ? ? ? //輸出排序完成后的數(shù)組
? ? {
? ? ? ? Console.Write("{0} ",nums[i]);
? ? }
? ? Console.ReadKey();? ? ? ? ? ? ? ? //暫停程序,按任意鍵結(jié)束暫停
執(zhí)行程序厨诸,程序的結(jié)果如圖所示:
8.數(shù)組元素的調(diào)換
? ? ? ? 將數(shù)組中的元素首尾交換
? ? string t=" ";? ? ? ? ? ? //定義一個(gè)字符串并賦初值
? ? string[] str = { "a", "b", "c","d","e","f" };
? ? for (int i = 0; i < str.Length/2; i++)? ? ? ? ? ? ? //str.Length調(diào)用Length方法镶殷,計(jì)算數(shù)組str[]的長度? ? ? ? ?
? ? {
? ? ? ? t = str[i];? ? ? ? ? ? //使用第三方變量交換兩個(gè)數(shù)的值
? ? ? ? str[i]=str[str.Length-1-i];
? ? ? ? str[str.Length-1-i] = t;
? ? }
? ? for (int i = 0; i < str.Length; i++)
? ? {
? ? ? ? Console.Write(str[i]);
? ? }
? ? Console.ReadKey();
執(zhí)行程序,程序的結(jié)果如圖所示:
9.數(shù)組元素的輸入輸出
? ? ? ? 輸出數(shù)組中元素的和微酬,最大值绘趋,最小值。
? ? int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, };? ? ? ? ? ? //定義一個(gè)整型數(shù)組并賦值
? ? int max = a[0];? ? ? ? ? ? ? ? ? ? //用數(shù)組中的元素賦值颗管,防止輸出的結(jié)果不是數(shù)組中的元素
? ? int min = a[0];
? ? int sum=0;
? ? for (int i = 0; i < a.Length; i++)
? ? {
? ? ? ? if (a[i] > max)? ? ? ? //取出數(shù)組中的最大值
? ? ? ? {
? ? ? ? ? ? max = a[i];
? ? ? ? }
? ? ? ? if (a[i] < min)? ? ? ? ? ? ? ? //取出數(shù)組中的最小值
? ? ? ? {
? ? ? ? ? ? min = a[i];
? ? ? ? }
? ? ? ? sum +=a[i];? ? ? ? ? ? //將數(shù)組中的元素求和
? ? }
? ? Console.WriteLine("{0},{1},{2}",sum,max,min);
執(zhí)行程序陷遮,程序的結(jié)果如圖所示:
10.面向?qū)ο?/h4>
? ? ? ? namespace _01_面向?qū)ο?/p>
? ? ? ? {
? ? ? ? ? ? public class Person? ? //類不占內(nèi)存垦江,對象占內(nèi)存帽馋,所以加載該項(xiàng)目時(shí),類不被加載運(yùn)行
? ? ? ? ? ? {
? ? ? ? ? ? ? ? public string? _names;? ? ? ? ? ? ? //字段
? ? ? ? ? ? ? ? public int _age;
? ? ? ? ? ? ? ? public string? _gender;
? ? ? ? ? ? ? ? public void CHLSS()? ? ? ? ? ? ? ? //定義一個(gè)方法
? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //this表示當(dāng)前這個(gè)類的對象
? ? ? ? ? ? ? ? ? ? Console.WriteLine("我叫{0},我今年{1}歲了绽族,我是{2}生", this._names, this._age, this._gender);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? public void CHLSSOne()? ? ? ? ? ? ? ? //定義一個(gè)方法姨涡,類中可以包含多個(gè)方法
? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //this表示當(dāng)前這個(gè)類的對象
? ? ? ? ? ? ? ? ? ? Console.WriteLine("我叫{0},我今年{1}歲了项秉,我是{2}生", this._names, this._age, this._gender);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
對類的調(diào)用
? ? ? ? using _01_面向?qū)ο?? ? ? ? ? ? ? ? ? ? //使用該命名空間下的類
? ? ? ? //類的實(shí)例化
? ? ? ? ? ? Person ZhangSan = new Person();? ? ? ? //使用之前先聲明一個(gè)Person類的對象,對象調(diào)用類中的東西(屬性绣溜,字段,方法)
? ? ? ? //聲明聲明一個(gè)對象的話要先聲明一個(gè)類娄蔼,對象包含各種屬性怖喻,字段,方法
? ? ? ? ZhangSan._names = "張三";? ? ? ? //為類中的字段賦值
? ? ? ? ZhangSan._age = 24;? ? ? ? //為類中的字段賦值
? ? ? ? ZhangSan._gender = "男";? ? ? ? //為類中的字段賦值
? ? ? ? ZhangSan.CHLSS();? ? ? //調(diào)用類中的方法? 對象.方法名
? ? ? ? Console.ReadKey();
? ? ? ? Person LiSi = new Person();
? ? ? ? //實(shí)例化一個(gè)對象李四? 之后的操作就只針對對象操作
? ? ? ? LiSi._names = "李四";
? ? ? ? LiSi._age = 18;
? ? ? ? LiSi._gender = "女";
? ? ? ? LiSi.CHLSS();
? ? ? ? Console.ReadKey();
? ? ? ? Person WangWu = new Person();? ?
? ? ? ? ? //實(shí)例化一個(gè)對象王五岁诉,WangWu就相當(dāng)于一個(gè)變量占內(nèi)存锚沸,Person相當(dāng)于int不占內(nèi)存
? ? ? ? WangWu._names = "王五";? ? ? ? ? //實(shí)際上給字段賦了初值,在此設(shè)斷點(diǎn)觀察WangWud的值
? ? ? ? WangWu._age = 19;
? ? ? ? WangWu._gender = "AB";
? ? ? ? WangWu.CHLSSOne();? ? ? ? ? //調(diào)用另外一個(gè)方法涕癣,類中可以包含多個(gè)方法
? ? ? ? Console.ReadKey();
執(zhí)行程序哗蜈,程序的結(jié)果如圖所示:
? ? ? ? 從輸出可以看出王五的性別輸出有誤坠韩,原因是沒有在類中對字段的賦值及取值進(jìn)行限定距潘。類中可以包含三部分字段、屬性只搁、方法音比。其中屬性就是用來限定字段的賦值和取值的,要用到get();? set();方法氢惋。get();方法用來限定字段的取值洞翩,set();方法用來限定什么樣的值才能賦值給字段。另外說一下類與結(jié)構(gòu)的相似點(diǎn)及區(qū)別焰望。
? ? ? ? struct結(jié)構(gòu)與類都可以包含字段骚亿、屬性、方法三部分熊赖。但是結(jié)構(gòu)是面向過程的方法来屠,而類是面向?qū)ο蟮姆椒ǎY(jié)構(gòu)不包含類的三個(gè)基本特征:封裝震鹉、繼承的妖、多態(tài)。