1、实例3-1奇偶数判断(if...else语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_1 : Form { public 实例3_1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //提取用户输入并转换为整数 int num = Convert.ToInt32(textBox1.Text); if (num % 2 == 0) lblShow.Text = num + "是偶数!"; else lblShow.Text = num + "是奇数!"; } } }
2、实例3_2体型判断(if...else if语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_2 : Form { public 实例3_2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { double h, w, t; w = Convert.ToDouble(textBox1.Text); //提取用户输入并转换为double h = Convert.ToDouble(textBox2.Text); t = w / (h * h); if (t < 18) { label3.Text = "您的体型偏瘦,要注意营养!"; } else if (t < 25) { label3.Text = "您的体型很标准,要注意保持!"; } else if (t < 27) { label3.Text = "您的体型偏胖,要多注意运动!"; } else label3.Text = "您的体型太胖了,要注意锻炼身体!"; } } }
3、实例3_3成绩转换为等级(switch语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_3 : Form { public 实例3_3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { double score = Convert.ToDouble(textBox1.Text); //提取用户输入的成绩并转换为浮点数 switch((int)score/10) //取出成绩的百位和十位,根据百位和十位确定等级 { case 10: case 9: label2.Text += "优"; break; case 8: label2.Text += "良"; break; case 7: label2.Text += "中"; break; case 6: label2.Text += "及格"; break; default: label2.Text += "不及格"; break; } } } }
4、实例3_4嵌套的分支演示

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_4 : Form { public 实例3_4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { char c = Convert.ToChar(textBox1.Text); //字符串转换为字符型 if(Char.IsLetter(c)) //判定指定的字符是否是一个字母 { if(Char.IsLower(c)) { label2.Text = "它是一个小字母."; } else if (Char.IsUpper(c)) { label2.Text = "它是大写字母."; } else { label2.Text = "它是中文字符."; } } else if (char.IsNumber(c)) { label2.Text = "它是数字."; } else { label2.Text = "它不是语言文字,也不是数字."; } } } }
5、实例3_5求1+2+3+...+100。(While语句)

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace 第二章_实例 12 { 13 public partial class 实例3_5 : Form 14 { 15 public 实例3_5() 16 { 17 InitializeComponent(); 18 } 19 20 private void 实例3_5_Load(object sender, EventArgs e) 21 { 22 int i, sum; 23 i = 1; //为循环变量赋初值 24 sum = 0; 25 while(i<=100) //循环条件 26 { 27 sum = sum + i; 28 i++; //改变循环变量值 29 } 30 label1.Text = "1到100的自然数之和是:" + sum; 31 } 32 } 33 }
6、实例3_6判断字符串英文字母个数(do...while语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_6 : Form { public 实例3_6() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int n = 0, i = 0; do { char c = textBox1.Text[i++]; if (c > 'A' && c < 'Z' || c > 'a' && c < 'z') n++; } while (i != textBox1.Text.Length); label2.Text += String.Format("{0}个。", n); } } }
7、实例3_7陌生人同富翁换钱小程序(for语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_7 : Form { public 实例3_7() { InitializeComponent(); } private void 实例3_7_Load(object sender, EventArgs e) { int i; double t, s1, s2; s1 = t = 0.01; //百万富翁第一天给陌生人的钱为1分 s2 = 100000; //陌生人第一天给百万富翁的钱为十万元 for(i=2;i<=30;i++) { t = t * 2; //百万富翁第i天给陌生人的钱 s1 = s1 + t; //百万富翁第i天共给陌生人的钱 s2 = s2 + 100000; //陌生人第i天后共给陌生人的钱 } label1.Text=String.Format("百万富翁给陌生人:{0:N2}元。\n" +"陌生人给百万富翁:{1:N2}元。",s1,s2); } } }
8、实例3_8添加查询联系人模拟程序(foreach语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_8 : Form { public 实例3_8() { InitializeComponent(); } struct Contacter //定义结构体 { public string name; public string telphone; } Contacter[] persons = new Contacter[10]; //定义结构体数组,用于保存联系人信息 int i = 0; //用来记录已添加的联系人的个数 private void button1_Click(object sender, EventArgs e) { //获得用户输入并保存到第i个数组元素中 persons[i].name = textBox1.Text; persons[i].telphone = textBox2.Text; i++; label5.Text = "已成功添加一个联系人!"; } private void button2_Click(object sender, EventArgs e) { bool isSearched = false; //定义标志变量,用于记录查找是否成功 foreach(Contacter c in persons) //迭代查找指定联系人 { if(c.name==textBox3.Text.Trim()) { isSearched = true; //修改标志变量,表示查询成功 label5.Text = "查找成功!此人电话号码为:" + c.telphone; } } if (!isSearched) label5.Text = "查无此人!"; } } }
9、实例3_9九九乘法表(循环嵌套语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_9 : Form { public 实例3_9() { InitializeComponent(); } private void 实例3_9_Load(object sender, EventArgs e) { label1.Text = "九九乘法表:\n"; for(int i=1;i<=9;i++) { for(int j=1;j<=i;j++) { label1.Text += String.Format("{0}*{1}={2,-2:D}", i, j, i * j); /*说明:在格式字符"{2,-2:D}"中,第一个"2"表示索引, * "-2:D"表示输出十进制数字,左对齐同时占两个字符位置, * 如果参数不足两位,则自动补充显示空格 */ } label1.Text += "\n"; } } } }
10、实例3_10判断质数(break语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_10 : Form { public 实例3_10() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int num = Convert.ToInt32(textBox1.Text); //把输入的文本转换成对应的整数 int n = (int)Math.Sqrt(num); //Math.Sqrt()方法求指定数字的平方根 int i; for (i=2;i<=n;i++) { if (num % i == 0) break; //不是质数,跳出循环体 } if (i <= n) //如果i<n,一定是在循环体内遇到break退出的,说明num不是质数 label2.Text = num + "不是质数!"; else label2.Text = num + "是质数!"; } } }
11、实例3_11过滤重复字符(continue语句)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 实例3_11 : Form { public 实例3_11() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { char ch_old, ch_new; ch_old = ' '; label2.Text += "\n\n"; for (int i = 1; i < textBox1.Text.Length;i++) { ch_new = (char)textBox1.Text[i]; if (ch_new == ch_old) continue; //前后两个字符相同,忽略后面的字符 label2.Text += ch_new.ToString(); ch_old = ch_new; } } } }
12、课后练习(查询字符串中的重复词,及重复次数)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 第二章_实例 { public partial class 课后练习 : Form { public 课后练习() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int n = 0; //记录出现重复的词汇的个数 string[] words = new string[10]; //保存出现重复的词汇 int[] times = new int[10]; //记录每一个重复的词汇出现的次数 //寻找第n个出现重复的词汇 for(int i=0;i<textBox1.Text.Length-2;i++) { bool isSame = false; //记录是否发生重复 string source = textBox1.Text.Substring(i, 2); //提取二字源词 int j = i + 2; while (j<textBox1.Text.Length-2) { string target = textBox1.Text.Substring(j, 2); //提取二字目标词 if(source==target) { times[n]++; //重复次数增加1 //如果是新出席那的重复词汇,则保存 if(Array.IndexOf(words,target)==-1) { isSame = true; words[n] = target; } } j++; } if (isSame) n++; //出现重复的词汇的个数加1 } label2.Text = String.Format("一共有{0}个重复的词汇!\n\n其中,", n); for (int i = 0; i < 10; i++) { if (!String.IsNullOrEmpty(words[i])) label2.Text += String.Format("“{0}”重复{1}次", words[i], times[i] + 1); } } private void 课后练习_Load(object sender, EventArgs e) { } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?