收藏 分销(赏)

C#代码-课后习题代码.doc

上传人:仙人****88 文档编号:11987997 上传时间:2025-08-25 格式:DOC 页数:17 大小:156.50KB 下载积分:10 金币
下载 相关 举报
C#代码-课后习题代码.doc_第1页
第1页 / 共17页
C#代码-课后习题代码.doc_第2页
第2页 / 共17页


点击查看更多>>
资源描述
第一章 :习题一、 习题二 using System; using System.Collections.Generic; namespace WindowsFormsApplication2 using System.Linq;using System.Text; public Form1(){InitializeComponent();} namespace ConsoleApplication1 private void button1_Click(object sender, EventArgs e) {class Program { label1.Font =new Font("华文彩云",80); {static void Main(string[] args) label1.ForeColor=System.Drawing.Color.Blue; { Console.WriteLine("Hello World!"); button1.Enabled=false; }}} Console.Read(); } } }} 第二章 习题二 private void button1_Click(object sender, EventArgs e){ String s, a; s = Convert.ToString(textBox1.Text); a = s.ToUpper(); textBox1.Text = a.ToString();} private void button2_Click(object sender, EventArgs e) { String s, a; s = Convert.ToString(textBox1.Text); a = s.ToLower();textBox1.Text = a.ToString(); } } } 习题六 private void button1_Click(object sender, EventArgs e) {string a; a = Convert.ToString(textBox1.Text); MessageBox.Show(Convert.ToString(a)+",欢迎进入C#的世界!", "欢迎进入C#的世界!", MessageBoxButtons.OK, MessageBoxIcon.Information);} 习题三 private void button1_Click(object sender, EventArgs e) { int a, b, c, d, x; x = Convert.ToInt32(textBox1.Text); a = x/1000; b = x/100-a*10; c = x/10-a*100-b*10; d = x%10; textBox2.Text=Convert.ToString(a); textBox3.Text= Convert.ToString(b); textBox4.Text=Convert.ToString(c); textBox5.Text=Convert.ToString(d); } 习题四银行贷款的月还额 private void button1_Click(object sender, EventArgs e) { double p, r, a, b; int n; p = Convert.ToDouble(textBox1.Text); r = Convert.ToDouble(textBox2.Text); n = Convert.ToInt32(textBox3.Text); b = Math.Pow((1 + r), n); a = p * r * b / (b - 1); textBox4.Text = a.ToString(".##"); } 习题五、随机数 private void button1_Click(object sender, EventArgs e) { Random intra = new Random(); int a=intra.Next(1, 101); char b = (char)a; textBox1.Text = Convert.ToString(a); textBox2.Text = Convert.ToString(b); } 第三章 习题二是否是闰年 private void button1_Click(object sender, EventArgs e) { int a; a=Convert.ToInt32(textBox1.Text); if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) label2.Text = "闰年"; else label2.Text = "不是闰年"; } 习题三一元二次方程组 private void button1_Click(object sender, EventArgs e) { int a, b, c;double x; a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text); c = Convert.ToInt32(textBox3.Text); x=Math.Pow(b,2); if (a == 0) abel4.Text = "不是一元二次方程"; else { if (x - 4 * a * c == 0) label4.Text = "有两个相等的实根"; else if (x - 4 * a * c > 0) label4.Text = "有两个不等的实根"; else label4.Text = "无解";} } 习题四 private void button1_Click(object sender, EventArgs e) { double a, b, c, m, s; a = Convert.ToDouble(textBox1.Text); b = Convert.ToDouble(textBox2.Text); c = Convert.ToDouble(textBox3.Text); m = (a + b + c) / 2; if (a > 0 && b > 0 && c > 0) { if (a + b > c && a + c > b &&b + c > a) { s = Math.Sqrt(m * (m - a) * (m - b) * (m - c)); label4.Text = "三角形面积为" + s; } else { MessageBox.Show("输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); label4.Text = ""; } } else { MessageBox.Show("输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); label4.Text = ""; } } 习题一、 private void button1_Click(object sender, EventArgs e) { int a, b, c, max, min; a = Convert.ToInt32(textBox1.Text); b = Convert.ToInt32(textBox2.Text); c = Convert.ToInt32(textBox3.Text); max = (a > b ? a : b) > c ? (a > b ? a : b) : c; min = (a < b ? a : b) < c ? (a <b ? a : b) : c; b = a + b + c - max - min; label5.Text = max+","+b+","+min; } 第四章 习题三 private void button1_Click(object sender, EventArgs e) { int s=0; for(int i=1;i<=100;i++) for (int j = 0; j <= i; j++) {s = s + j;} textBox1.Text = Convert.ToString(s); } private void button1_Click(object sender, EventArgs e) { int s = 0,u=0; for (int i = 1; i <= 100; i++) { u=u+i-1; s = s+u+i; } textBox1.Text = Convert.ToString(s); } 习题五、3和7的倍数 private void button1_Click(object sender, EventArgs e) { for (int i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 7 == 0) textBox1.Text = textBox1.Text + "\r\n" + Convert.ToString(i); } 习题一 private void button1_Click(object sender, EventArgs e) { int s=0; for (int i = 1; i <= 100; i++) { s = s + i; } textBox1.Text = Convert.ToString(s) 第五章习题六 private void button1_Click(object sender, EventArgs e) { int[] a; int i, n; n = Convert.ToInt32(textBox1.Text); BestPerson f = new BestPerson(); f.getBestPosition(n, out a); for (i = 0; i < n; i++) textBox3.Text = textBox3.Text + Convert.ToString(a[i]); } } public partial class BestPerson { public void getBestPosition(int a, out int[] s) { s = new int[50]; int i, j = -1, k; for (i = 0; i < a; i++) { s[i] = 0; } for (i = 1; i <= a; i++) { k = 0; do{j++; if (j == a) j = 0; if (s[j] == 0) k++; } while (k != 7); s[j] = i; }} 第五章习题八 private void button1_Click(object sender, EventArgs e) { Student s = new Student(); MasterStudent m = new MasterStudent(); m.Age = Convert.ToInt32(textBox2.Text); m.Name = textBox1.Text; m.Number = textBox4.Text; m.Sex = textBox3.Text; m.Major = textBox5.Text; m.Teacher = textBox6.Text; if (m.UpdateAge(m.Age)) m.sayHallo(); else MessageBox.Show("年龄有错!"); } } public class Student {private string number;public string Number { get { return number; } set { number = value; }} private string name; public string Name { get { return name; }set { name = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } private int age; public int Age { get { return age; } set { age = value; }} public Boolean UpdateAge(int a) { if (a > 0 && a <= 120) { age = a; return true; } else return false;} public void sayHallo() { MessageBox.Show("我是一名学生,我的名字是:" + Name.ToString() + ",年龄: " + Age.ToString() + ",性别:" + Sex.ToString() + ",学号:" + Number.ToString()); }} public class MasterStudent : Student{ public string Major; public string Teacher; public new void sayHallo() { MessageBox.Show("我是一名研究生,我的名字是:" + Name.ToString() + ",年龄:" + Age.ToString() + ",性别:" + Sex.ToString() + ",学号:" + Number.ToString() + ",方向:" + Major + ",指导老师" + Teacher);} }} 习题5 int fac(int n) { if (n > 0) { if (n == 1) return (1); else return (n * fac(n - 1)); } else return 1; } private void button1_Click(object sender, EventArgs e) { int n, s; n = Convert.ToInt32(textBox1.Text); s = fac(n); textBox2.Text = Convert.ToString(s); } } } 第六章 习题七 private void button1_Click(object sender, EventArgs e) { int i; party[] p = new party[3]; party p1 = new party(); communistParty p2 = new communistParty(); kuomintang p3 = new kuomintang(); p[0] = p1; p[1] = p2; p[2] = p3; for (i = 0; i < 3; i++) p[i].sayPolicy();}} public class party { public virtual void sayPolicy(){ MessageBox.Show("两岸同属于一个中国。"); }} public class communistParty :party{ public override void sayPolicy(){ MessageBox.Show("两岸同属于一个中国,用一国两制统一中国。"); }} public class kuomintang :party{ public override void sayPolicy(){ MessageBox.Show("两岸同属于一个中国,用三民主义统一中国。"); } }} private void button1_Click(object sender, EventArgs e) { Cat c = new Cat(); c.name = textBox1.Text; Mouse m = new Mouse(); m.name = textBox2.Text; c.getUp +=new sayGetUpDelegate(m.sayGetUp); c.crow();}} 习题八 public delegate void sayGetUpDelegate(); public class Cat { public event sayGetUpDelegate getUp; public string name; public void crow() { MessageBox.Show(name+"喵~喵~喵~喵~喵~喵~"); getUp(); } } public class Mouse { public string name; public void sayGetUp () { MessageBox.Show(name+"快跑!!!!"); } } } 习题九 private void button1_Click(object sender, EventArgs e) { train t = new train(); t.name = textBox1.Text; cat c = new cat(); c.name = textBox2.Text; c.h = Convert.ToInt16(textBox3.Text); t.nshow(); c.nshow();} private void button2_Click(object sender, EventArgs e) { train t = new train(); cat c = new cat(); t.shout(); c.shout();}} public interface makeVoice{ int h{get;set;} void shout();} public abstract class Vehicle { public string name; public abstract void nshow();} public abstract class Anima{ public string name; public abstract void nshow();} public class train:Vehicle ,makeVoice{ int H; public int h { get { return this.H; } set { this.H = value; }} public override void nshow(){MessageBox.Show("train的名字是"+name); } public void shout(){MessageBox.Show("train" +"呜呜嗒嗒。。。。。。呜呜嗒嗒");} } public class cat:Animal ,makeVoice{ int H; public int h{ get { return this.H; } set { this.H = value; } } public override void nshow(){ MessageBox.Show("cat的名字是" + name+"身高是"+h); } public void shout(){ MessageBox.Show("cat" +"喵喵喵。。。。。。喵喵喵");}}} 第七章 习题一 随机产生10个两位整数,找出其中最大值,最小值、高于平均数的数 private void button1_Click(object sender, EventArgs e) { int sum = 0; int z; Random ra = new Random(); int[] a = new int[10]; for (int i = 0; i < a.Length; i++) { a[i] = ra.Next(10, 100); } for (int i = 0; i < a.Length; i++){ textBox1.Text += Convert.ToString(a[i]+" ") }; for (int i = 0; i < a.Length; i++) { sum += a[i] }; z = sum / 10; textBox4.Text = Convert.ToString(z) for (int i = 0; i < a.Length; i++) { if (a[i] >= z) { textBox5.Text += Convert.ToString(a[i] + " "); } } Array.Sort(a); textBox2.Text = Convert.ToString(a[9]); textBox3.Text = Convert.ToString(a[0]); } 产生10个学生的成绩,并用冒泡进行排序 private void button1_Click(object sender, EventArgs e) { Random ra = new Random(); int[] a = new int[10]; for (int i = 0; i < a.Length; i++){ a[i] = ra.Next(1, 100); textBox1.Text += Convert.ToString(a[i] + " ");} for (int i = 0; i < a.Length-1; i++) { for (int j = i + 1; j < a.Length; j++) { if (a[i] < a[j]) { int t; t = a[i]; a[i] = a[j]; a[j] = t; } }} for (int i = 0; i < a.Length; i++) { textBox2.Text += Convert.ToString(a[i] + " "); } } 交错数组 private void button1_Click(object sender, EventArgs e) { int[][] jcArray = new int[4][]; jcArray[0] = new int[] { 1, 32, 89, 6, 8 }; jcArray[1] = new int[] { 2,17,8 }; jcArray[2] = new int[] { 3,5}; jcArray[3] = new int[] { 4,9,23,12 }; textBox1.Text = "交错数组的数据:" + "\r\n"; foreach (int[] j in jcArray) { int x = 0; foreach (int i in j) { x += i; textBox1.Text += Convert.ToString(i) + "\t"; } textBox1.Text += "\r\n"+"该行数据总和为:"+Convert.ToString(x)+"\r\n"; }}}} 用二维数组存储学生成绩 private void button1_Click(object sender, EventArgs e) { int[,] cj = { { 1001, 93 }, { 1004, 78 }, { 1002, 59 }, { 1005, 87 }, { 2001, 55 } }; textBox1.Text = "学生成绩"; for(int i=0;i<cj .GetLength(0);i++) for (int j = 0; j < cj.GetLength(1); j++) { if (j == 1) textBox1.Text = textBox1.Text + "\t" + Convert.ToString(cj[i, j]); else textBox1.Text = textBox1.Text + "\r\n" + Convert.ToString(cj[i, j]); } textBox1.Text = textBox1.Text + "\r\n" + "不及格的学生"; for (int i = 0; i < cj.GetLength(0); i++) if (cj[i, 1] < 60) textBox1.Text = textBox1.Text + "\r\n" + Convert.ToString(cj[i, 0])+ "\t"+Convert .ToString(cj[i,1]) textBox1.Text+="\r\n"+"平均成绩"; int sum = 0; int z; ; for (int i=0;i<cj.GetLength(0);i++) { sum += cj[i, 1]; } z = sum / cj.GetLe
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服