当前位置:文档之家› 第四版 c#程序设计教程(课后习题答案代码)

第四版 c#程序设计教程(课后习题答案代码)

判断是否是闰年课本63

用户输入整数反向显示课本67或68

乘法表课本69

判断从键盘输入大于3的整数是否为素数课本70

求输入所以数其中正数的和课本70

求1平方+2平方+……+n平方小于等于1000 的最大n 课本71或72

读入一组数(以0结束),分别求奇数和偶数和

static void Main(string[] args)

{int n,s1=0,s2=0;

do

{ n = int.Parse(Console.ReadLine());

if (n%2==1)

s1 += n;

else

s2 += n;

} while (n!=0);

Console.WriteLine("奇数之和={0}",s1);

Console.WriteLine("偶数之和={0}",s2);

}

输入正整数n计算S=1+(1+2)+(1+2+3)+……+(1+2+….+n)

int n,i,j,s=0;

Console.Write("n:");

n = int.Parse(Console.ReadLine());

for (i = 1; i <= n; i++)

for (j = 1; j <= i; j++)

s += j;

Console.WriteLine("s={0}", s);

杨辉三角

static void Main(string[] args)

{int i,j,c,n;

Console.Write("n:");

n=int.Parse(Console.ReadLine());

if (n>13)

Console.WriteLine("输入的数值太大!");

else

{for (i=0;i<=n-1;i++)

{ for (j=1;j<15-i;j++)

Console.Write(" "); //每次循环显示2个空格 c=1;

Console.Write("{0} ",c);

for (j=1;j<=i;j++)

{ c=c*(i-j+1)/j;

if (c<100)

if (c<10)

Console.Write("{0} ",c); //显示3个空格

else

Console.Write("{0} ",c); //显示2个空格

else

Console.Write("{0} ",c); //显示1个空格

}

Console.WriteLine();

}

计算π的值

double pi=0.0;

int i;

for (i=1;i<=2000;i++)

if (i%2==1)

pi=pi+1.0/(2*i-1);

else

pi=pi-1.0/(2*i-1);

pi=4*pi;

Console.WriteLine("π={0}", pi);

求水仙花数

static void Main(string[] args)

{ int i, n, a, b, c;

for (i = 100; i <= 999; i++)

{ n = i;

c = n % 10; n = n / 10;

b = n % 10; n = n / 10;

a = n;

if (a * a * a + b * b * b + c * c * c == i)

{ Console.WriteLine("{0} {1} {2} = {3}", a, b, c,a*a*a+b*b*b+c*c*c);

//Console.Write("{0} ", i);

}

}

Console.WriteLine();

}

假设10个整数用一维数组存放,求最大值和次大值

static void Main(string[] args)

{ int[] a = new int[10]{1,8,10,4,7,9,6,10,2,5};

int n=10,max1,max2,i;

max1=a[0]>a[1]?a[0]:a[1];

max2=a[0]>a[1]?a[1]:a[0];

for (i=2;i

if (max1

{ max2=max1;

max1=a[i];

}

Console.WriteLine("max1={0},max2={1}",max1,max2);

}

用一个二维数组存放5个考生4门功课的考试成绩,求每位考生的平均成绩课本89页

static void Main(string[] args)

{ const int Max = 5; //考生数

int[] Ave = new int[Max]; //定义一个一维数组存储考生的总成绩

int[,] grade={{88,75,62,84},{96,85,75,92}, //定义二维数组存储考生成绩

{68,63,72,78},{95,89,76,98},

{76,65,72,63}};

for(int i=0; i

{ for(int j=0; j<4; j++)

{ Ave[i] += grade[i,j]; //累加考生成绩

}

}

for (int k = 0; k < Max; k++)

Console.WriteLine("考生{0}平均成绩={1} ",k+1, Ave[k]/4.0);

}

用俩个一维数组分别存放5个学生的学号和姓名,分别按学号和姓名排序课本89页上级实验5

class Program

{ const int Max = 5;

static void disp(int[] no,string[] name,string str)

{ Console.WriteLine(str);

Console.Write("学号:\t");

for (int i = 0; i < no.Length; i++)

Console.Write("{0}\t",no[i]);

Console.WriteLine();

Console.Write("姓名:\t");

for (int i = 0; i < name.Length; i++)

Console.Write("{0}\t", name[i]);

Console.WriteLine();

}

static void Main(string[] args)

{ int[] no = new int[] { 2, 4, 5, 1, 3};

string[] name = new string[] {"Smith","John","Mary","Cherr","Tomn"};

disp(no, name,"排序前:");

Array.Sort(no, name);

disp(no, name,"按学号排序后:");

Array.Sort(name, no);

disp(no, name, "按姓名排序后:");

}

课本124页8

class Program

{ static void Main(string[] args)

{ Person p1 = new Person(2, 50);

Animal a1 = new Animal();

p1.show();

a1.show();

}

}

public class Person//定义人类

{public int legs; //腿的只数

protected float weight; //重量

public Person() //默认构造函数

{ }

public Person(int legs1,float weight1)//自定义方法F { legs= legs1;

weight = weight1;

}

public void show()

{ Console.WriteLine("某人有{0}只腿,重量为{1}kg", legs, weight);

}

}

class Animal//定义动物类

{ public int num; //腿的条数

private float weight; //重量

public Animal() //Animal类的默认构造函数

{ }

public Animal(int n,float w) //Animal类带2个参数的构造函数 { num = n;

weight = w;

}

public void show()

{ Console.WriteLine("某动物有{0}只脚,重量为{1}kg", num, weight);

}

}

课本124页9

//定义了一个委托,委托在传递方法时,方法必须带两个int型的参数。

public delegate int Call(int num1, int num2);

//在Delegates类的内部定义Math类和TestDelegates类。

class Math

{ public int fun1(int num1, int num2)

{ return num1*num1+num2*num2;

}

public int fun2(int num1, int num2)

{ return num1*num1-num2*num2;

}

}

class Program

{ static void Main(string[] args)

{ int result;

Call objCall; //委托的对象

Math objMath = new Math(); //Math类的对象 objCall = new Call(objMath.fun1);

result = objCall(5, 3); //将委托实例化Console.WriteLine("结果为{0}", result);

objCall = new Call(objMath.fun2);

result = objCall(5, 3); //将委托实例化Console.WriteLine("结果为{0}", result);

}

}

课本124页10

class List

{ private int Max = 100; //存储最多元素

private int num = 0; //存储的实际元素个数

private object[] list; //存储元素数组

public List() //构造函数

{ list = new object[Max];

}

public void add(object obj) //添加一个元素

{ list[num] = obj;

num++;

}

public void delete(int pos) //删除一个元素

{ for (int i = pos + 1; i < num; i++)

list[i - 1] = list[i];

num--;

}

public object get(int pos) //获取指定位置的元素 { if (pos < num)

return list[pos];

else

return null;

}

public int getnum() //获取实际元素个数

{ return num;

}

public string disp() //获取所有元素

{ string s = "";

for (int i = 0; i < num; i++)

s += list[i] + " ";

return s;

}

}

class Program

{ static void Main(string[] args)

{ List list = new List();

list.add("abc");

list.add(1.23);

list.add(2);

list.add('a');

Console.WriteLine("元素序列:{0}",list.disp());

Console.WriteLine("元素个数:{0}",list.getnum());

Console.WriteLine("位置1的元素:{0}",list.get(1));

Console.WriteLine("删除位置2的元素");

list.delete(2);

Console.WriteLine("元素序列:{0}", list.disp());

}

}

课本124页11

public class Student

{ private string name;

private int eng, math, sum;

public int psum

{ get { return sum; }

}

public void inscore()

{ Console.Write("姓名:");

name = Console.ReadLine();

Console.Write("英语:");

eng = int.Parse(Console.ReadLine());

Console.Write("数学:");

math = int.Parse(Console.ReadLine());

sum = eng + math;

}

public void display()

{ Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", name, eng, math, sum); }

}

class Program

{ const int Max = 100;

static void sort(int n, params Student[] p) //采用冒泡排序法排序 { int i, j;

bool exchange;

Student tmp;

for (i = 0; i < n - 1; i++)

{ exchange = false;

for (j = n - 2; j >= i; j--)

if (p[j + 1].psum > p[j].psum)

{ tmp = p[j + 1]; //p[j+1]<->p[j] p[j + 1] = p[j];

p[j] = tmp;

exchange = true;

}

if (exchange == false)

break;

}

}

static void Main(string[] args)

{ int n, i;

Student[] p = new Student[Max]; //定义对象引用数组

Console.Write("n:");

n = int.Parse(Console.ReadLine());

for (i = 0; i < n; i++) //创建对象引用的实例 p[i] = new Student();

for (i = 0; i < n; i++)

{ Console.WriteLine("输入第{0}个学生数据:", i + 1); p[i].inscore();

}

Console.WriteLine("排序前:");

Console.WriteLine("\t姓名\t英语\t数学\t总分");

for (i = 0; i < n; i++)

{ Console.Write("序号{0}:", i + 1);

p[i].display();

}

sort(n, p); //按总降序排序

Console.WriteLine("排序后:");

Console.WriteLine("\t姓名\t英语\t数学\t总分");

for (i = 0; i < n; i++)

{ Console.Write("第{0}名:", i + 1);

p[i].display();

}

}

}

课本124上机实验6

class Student//学生类

{ int sno; //学号

string sname; //姓名

Course[] course; //Course类对象数组

int[] score; //课程成绩数组

double sgpa1; //常见GPA值

double sgpa2; //标准GPA值

public int psno //psno属性可读可写

{ get

{ return sno; }

set

{ sno = value; }

}

public string psname //psname属性可读可写

{ get

{ return sname; }

set

{ sname = value; }

}

public void setcourse(params Course[] course1) //设置课程

{ course = new Course[course1.Length];

for (int i = 0; i < course1.Length; i++)

course[i] = course1[i];

}

public void setscore(int[] score1) //设置分数

{ score = new int[score1.Length];

for (int i = 0; i < score1.Length; i++)

score[i] = score1[i];

}

public void computegpa() //根据课程的学分以及学生成绩计算GPA { int i;

double s, sumc = 0, sumgpa1 = 0, sumgpa2 = 0;

for (i = 0; i < score.Length; i++)

{ if (score[i] >= 90)

s = 4.0; //点数

else if (score[i] >= 80)

s = 3.0;

else if (score[i] >= 70)

s = 2.0;

else if (score[i] >= 60)

s = 1.0;

else

s = 0.0;

sumgpa1 += course[i].pcredits * s;

sumgpa2 += course[i].pcredits * score[i];

sumc += course[i].pcredits;

}

sgpa1 = sumgpa1 / sumc;

sgpa2 = sumgpa2 * 4 / sumc / 100;

}

public void dispstud() //输出学生信息

{ Console.WriteLine("学号:{0}\t姓名:{1}", sno, sname);

Console.WriteLine(" 课程名\t学分\t分数");

for (int i = 0; i < course.Length; i++)

Console.WriteLine(" {0}\t{1}\t{2}", course[i].pcname, course[i].pcredits, score[i]);

}

public void dispgpa() //输出GPA

{ Console.WriteLine("常见算法GPA={0:n},标准算法GPA={1:n}", sgpa1, sgpa2);

}

}

class Course//课程类

{ string cname; //课程名

int credits; //课程学分

public Course() { }

public Course(string name, int xf) //构造函数

{ cname = name;

credits = xf;

}

public string pcname //pcname属性,课程名可读可写

{ get

{ return cname; }

set

{ cname = value; }

}

public int pcredits //pcredits属性,课程学分可读可写

{ get

{ return credits; }

set

{ credits = value; }

}

}

class Program

{ static void Main(string[] args)

{ Course[] course1 = new Course[] {new Course("课程1",4),new Course("课程2",3), new Course("课程3",2),new Course("课程4",6),new Course("课程5",3)};

int[] score1 = new int[] { 92, 80, 98, 70, 89 };

Student s1 = new Student();

s1.psno = 1; s1.psname = "王华";

s1.setcourse(course1);

s1.setscore(score1);

https://www.doczj.com/doc/5a16423064.html,putegpa();

s1.dispstud();

s1.dispgpa();

}

}

课本157页7

using System;

using System.Collections.Generic;

using System.Text;

namespace Proj7_15

{ public class Employee

{ private double bsalary= 1000;

private double psalary;

private int n;

public int pn

{ get { return n; }

set { n = value; }

}

public double compsalary()

{ Console.Write("工作年数:");

pn = int.Parse(Console.ReadLine());

psalary = bsalary+30*pn;

return psalary;

}

}

public class UEmployee : Employee

{ new public double compsalary()

{ return 1.5 * https://www.doczj.com/doc/5a16423064.html,psalary();

}

}

class Program

{ static void Main(string[] args)

{ Employee emp1 = new Employee();

Console.WriteLine("该普通职工工资:{0}", https://www.doczj.com/doc/5a16423064.html,psalary());

UEmployee emp2 = new UEmployee();

Console.WriteLine("该本科生职工工资:{0}", https://www.doczj.com/doc/5a16423064.html,psalary()); }

}

}

课本157页8

public class Employee//普通职工类

{ private double bsalary = 1000; //基本工资

private double psalary; //实际工资

private int n; //工作年数

public int pn

{get { return n; }

set { n = value; }

}

public virtual double compsalary() //计算普通员工工资

{ Console.Write("工作年数:");

pn = int.Parse(Console.ReadLine());

psalary = bsalary + 30 * pn;

return psalary;

}

}

public class UEmployee : Employee//本科生职工类

{ public override double compsalary()

{ return 1.5 * https://www.doczj.com/doc/5a16423064.html,psalary();

}

}

public class GEmployee : Employee//研究生职工类

{ public override double compsalary()

{ return 2 * https://www.doczj.com/doc/5a16423064.html,psalary();

}

}

class Program

{ static void Main(string[] args)

{ Employee emp1 = new Employee();

Console.WriteLine("该普通职工工资:{0}", https://www.doczj.com/doc/5a16423064.html,psalary());

UEmployee emp2 = new UEmployee();

Console.WriteLine("该本科生职工工资:{0}", https://www.doczj.com/doc/5a16423064.html,psalary());

GEmployee emp3 = new GEmployee();

Console.WriteLine("该研究生职工工资:{0}", https://www.doczj.com/doc/5a16423064.html,psalary()); }

}

课本157页9

public class Person//人类

{ private int no; //编号

private string name; //姓名

public void input()

{ Console.Write(" 编号:");

no = int.Parse(Console.ReadLine());

Console.Write(" 姓名:");

name = Console.ReadLine();

}

public void disp()

{ Console.WriteLine(" 编号:{0}",no);

Console.WriteLine(" 姓名:{0}",name);

}

}

public class Student : Person//学生类

{ private string sclass; //班号

private int degree; //成绩

public void input()

{ base.input();

Console.Write(" 班号:");

sclass = Console.ReadLine();

Console.Write(" 成绩:");

degree = int.Parse(Console.ReadLine());

}

new public void disp()

{ base.disp();

Console.WriteLine(" 班号:{0}",sclass);

Console.WriteLine(" 成绩:{0}",degree);

}

}

public class Teacher : Person//教师类

{ private string prof; //职称

private string depart; //部门

public void input()

{ base.input();

Console.Write(" 职称:");

prof = Console.ReadLine();

Console.Write(" 部门:");

depart = Console.ReadLine();

}

new public void disp()

{ base.disp();

Console.WriteLine(" 职称:{0}", prof);

Console.WriteLine(" 部门:{0}", depart);

}

}

class Program

{ static void Main(string[] args)

{ Student s1 = new Student();

Teacher t1 = new Teacher();

Console.WriteLine("输入一个学生数据:"); s1.input();

Console.WriteLine("输入一个教师数据:"); t1.input();

Console.WriteLine("显示一个学生数据:"); s1.disp();

Console.WriteLine("显示一个教师数据:"); t1.disp(); }

}

课本157页10

public class Student:IComparable

{ private string name;

private int eng, math, sum;

public int psum

{ get { return sum; }

}

public void inscore()

{ Console.Write("姓名:");

name = Console.ReadLine();

Console.Write("英语:");

eng = int.Parse(Console.ReadLine());

Console.Write("数学:");

math = int.Parse(Console.ReadLine());

sum = eng + math;

}

public void display()

{ Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", name, eng, math, sum); }

public int CompareTo(object obj) //实现接口方法

{ Student s = (Student)obj; //转换为Student实例

if (psum < s.psum) return 1;

else if (psum == s.psum) return 0;

else return -1;

}}

class Program

{static void Main(string[] args)

{ int n, i;

ArrayList myarr = new ArrayList();

Student p; //定义对象引用

Console.Write("n:");

n = int.Parse(Console.ReadLine());

for (i = 0; i < n; i++)

{Console.WriteLine("输入第{0}个学生数据:", i + 1);

p = new Student(); //创建对象引用实例

p.inscore();

myarr.Add(p);

}

Console.WriteLine("排序前:");

Console.WriteLine("\t姓名\t英语\t数学\t总分");

i = 1;

foreach(Student s in myarr)

{ Console.Write("序号{0}:", i++);

s.display();}

myarr.Sort(); //按总分降序排序

Console.WriteLine("排序后:");

Console.WriteLine("\t姓名\t英语\t数学\t总分");

i = 1;

foreach (Student s in myarr)

{ Console.Write("第{0}名:", i++);

s.display();

} } }

课本157页上机实验7

public class BClass//基类

{ private string name; //名称

private int no; //编号

public BClass(string na, int n) //构造函数

{ name = na; no = n;

}

public void show()

{ Console.Write("{0}({1})", name, no);

}

}

public class Book : BClass//图书类

{ string author; //作者

public Book(string na, int n, string auth)

: base(na, n)

{ author = auth;

}

public void showBook()

{ base.show();

Console.Write("作者:{0}", author);

} }

public class Reader : BClass//读者类

{ Book[] rent; //所借图书

int top;

public Reader(string na, int n)

: base(na, n) //构造函数

{ rent = new Book[5];

top = 0; }

public void rentBook(ref Book b)

{ rent[top] = b;

top++; }

public void showReader()

{ Console.Write("读者:");

base.show();

Console.WriteLine("所借图书:");

for (int i = 0; i < top; i++)

{ Console.Write(" {0}:", i + 1); //5个空格

rent[i].show();

Console.WriteLine();

}} }

class Program

{ static void Main(string[] args)

{ Book b1 = new Book("C语言", 100, "潭浩强");

Book b2 = new Book("数据结构", 110, "严蔚敏");

Book b3 = new Book("软件工程", 210, "陈华");

Book b4 = new Book("操作系统", 208, "张明");

Reader r1 = new Reader("王华", 1234);

Reader r2 = new Reader("李兵", 2600);

r1.rentBook(ref b1);

r1.rentBook(ref b2);

r1.rentBook(ref b3);

r2.rentBook(ref b4);

r1.showReader();

r2.showReader();

}}

课本200页3

public partial class Form1 : Form

{ int n = 0; //保存用户错误输入的次数

public Form1()

{ InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{ if (textBox1.Text == "1234" && textBox2.Text == "1234") { MessageBox.Show("用户名/口令正确");

this.Close();

//调用其他程序

}

else

{ if (n<3)

{ MessageBox.Show("用户名/口令错误,再次输入"); n++;

}

else

{ MessageBox.Show("已经错误输入三次,退出");

this.Close();}

}}

课本200页4

public partial class Form2 : Form

{ public Form2()

{ InitializeComponent();

}

private void rbutton1_CheckedChanged(object sender, EventArgs e)

{ Font f = new Font("宋体",textBox1.Font.Size,textBox1.Font.Style);

textBox1.Font = f;

}

private void rbutton2_CheckedChanged(object sender, EventArgs e)

{ Font f = new Font("楷体_GB2312", textBox1.Font.Size, textBox1.Font.Style);

textBox1.Font = f;

//https://www.doczj.com/doc/5a16423064.html, = "楷体_GB2312";

}

private void rbutton3_CheckedChanged(object sender, EventArgs e)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 10, textBox1.Font.Style);

textBox1.Font = f; }

private void rbutton4_CheckedChanged(object sender, EventArgs e)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 16, textBox1.Font.Style);

textBox1.Font = f;

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{ if (checkBox1.Checked)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, textBox1.Font.Size, FontStyle.Bold);

textBox1.Font = f;

}

else

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, textBox1.Font.Size, FontStyle.Regular); textBox1.Font = f;

} } }

课本200页 5

public partial class Form3 : Form

{ int a=0, b=0;

public Form3()

{InitializeComponent();

}

private void Form3_Load(object sender, EventArgs e)

{ listBox1.Items.Add("12");

listBox1.Items.Add("10");

listBox1.Items.Add("5");

listBox1.Items.Add("8");

listBox1.Items.Add("22");

listBox1.Items.Add("25");

listBox1.Items.Add("9");

listBox2.Items.Add("9");

listBox2.Items.Add("15");

listBox2.Items.Add("20");

listBox2.Items.Add("32");

listBox2.Items.Add("18");

listBox2.Items.Add("5");

listBox2.Items.Add("7");

listBox2.Items.Add("32");

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { a = int.Parse(listBox1.Text);

}

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)

{ b = int.Parse(listBox2.Text);

listBox3.Items.Add(string.Format("{0}×{1}={2}",a,b,a*b));

}

private void listBox3_SelectedIndexChanged(object sender, EventArgs e)

{ if (listBox3.SelectedIndex>=0)

listBox3.Items.RemoveAt(listBox3.SelectedIndex);

}

}

课本200页6

public partial class Form4 : Form

{ public Form4()

{ InitializeComponent();

}

private void Form4_Load(object sender, EventArgs e)

{ comboBox1.Items.Add("北京");

comboBox1.Items.Add("上海");

comboBox1.Items.Add("天津");

comboBox1.Items.Add("广州");

comboBox1.Items.Add("武汉");

comboBox1.Items.Add("沈阳");

comboBox1.Items.Add("合肥");

comboBox1.Items.Add("长沙");

comboBox1.Items.Add("重庆");

}

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)

{ //MessageBox.Show(e.KeyChar.ToString());

if (e.KeyChar == 13) //按Enter键后判断

{ if (comboBox1.Items.Contains(comboBox1.Text))

label2.Text = "你的输入已在组合框!";

else

{ comboBox1.Items.Add(comboBox1.Text);

label2.Text = "你的输入新项已添加到组合框中!";

} }}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{ MessageBox.Show(string.Format("你选择了'{0}'项",comboBox1.Text)); }} 课本200页上机实验8

public partial class Form5 : Form

{ struct StudType

{ public int no;

public string name;

public string sex;

public string sclass;

public DateTime rq; }

StudType[] stud = new StudType[10]; //定义一个结构类型的数组

public Form5()

{ InitializeComponent();}

private void Form5_Load(object sender, EventArgs e)

{ //给定义的数组赋初值

stud[0].no = 1; stud[0].name = "王华"; stud[0].sex = "男";

stud[0].rq = new DateTime(1980,2,10); stud[0].sclass = "99091";

stud[1].no = 2; stud[1].name = "李强"; stud[1].sex = "男";

stud[1].rq = new DateTime(1981,10,4); stud[1].sclass = "99101";

stud[2].no = 3; stud[2].name = "张丽"; stud[2].sex = "女";

stud[2].rq = new DateTime(1980,3,2); stud[2].sclass = "99091";

stud[3].no = 4; stud[3].name = "汪洋"; stud[3].sex = "男";

stud[3].rq = new DateTime(1980,1,5); stud[3].sclass = "99101";

stud[4].no = 5; stud[4].name = "江华"; stud[4].sex = "男";

stud[4].rq = new DateTime(1980,3,10); stud[4].sclass = "99091";

stud[5].no = 6; stud[5].name = "李英"; stud[5].sex = "女";

stud[5].rq = new DateTime(1980,6,2); stud[5].sclass = "99101";

stud[6].no = 7; stud[6].name = "胡军"; stud[6].sex = "男";

stud[6].rq = new DateTime(1981,10,9); stud[6].sclass = "99091";

stud[7].no = 8; stud[7].name = "刘驰"; stud[7].sex = "女";

stud[7].rq = new DateTime(1982,5,2); stud[7].sclass = "99101";

stud[8].no = 9; stud[8].name = "宋仁"; stud[8].sex = "男";

stud[8].rq = new DateTime(1980,8,3); stud[8].sclass = "99101";

stud[9].no = 10; stud[9].name = "许兵"; stud[9].sex = "男";

stud[9].rq = new DateTime(1980,11,8); stud[9].sclass = "99091";

//将学号都添加到组合框中

for(int i = 0;i<10;i++)

comboBox1.Items.Add(stud[i].no); }

private void button1_Click(object sender, EventArgs e)

{ if (comboBox1.Text != "")

{ int i = 0; //查找指定学号的学生记录

while (Convert.ToInt32(comboBox1.Text) != stud[i].no)

i = i + 1;

if (i >= 10)

MessageBox.Show("没有该学号的记录");

else

{ textBox1.Text = stud[i].no.ToString(); //显示找到的学生记录

textBox2.Text = stud[i].name;

textBox3.Text = stud[i].sex;

textBox4.Text = stud[i].sclass;

textBox5.Text = stud[i].rq.ToString();}

}}}

课本236页2

public partial class Form1 : Form

{public Form1()

{InitializeComponent();}

private void menu11_Click(object sender, EventArgs e)

{ Font f = new Font("宋体",label1.Font.Size,label1.Font.Style);

label1.Font = f; }

private void menu12_Click(object sender, EventArgs e)

{ Font f = new Font("仿宋_GB2312", label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu13_Click(object sender, EventArgs e)

{ Font f = new Font("黑体", label1.Font.Size, label1.Font.Style);

label1.Font = f; }

private void menu14_Click(object sender, EventArgs e)

{ Font f = new Font("幼圆", label1.Font.Size, label1.Font.Style);

label1.Font = f; }

private void menu15_Click(object sender, EventArgs e)

{ Font f = new Font("楷体_GB2312", label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu21_Click(object sender, EventArgs e)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 28, label1.Font.Style);

label1.Font = f; }

private void menu22_Click(object sender, EventArgs e)

{Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 20, label1.Font.Style);

label1.Font = f; }

private void menu23_Click(object sender, EventArgs e)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 16, label1.Font.Style);

label1.Font = f; }

private void menu24_Click(object sender, EventArgs e)

{ Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 12, label1.Font.Style);

label1.Font = f; }

private void menu25_Click(object sender, EventArgs e)

{Font f = new Font(https://www.doczj.com/doc/5a16423064.html,, 8, label1.Font.Style);

label1.Font = f; }

}

课本237页上机实验9

public partial class Form2 : Form

{ public Form2()

{InitializeComponent();}

private void Form2_Load(object sender, EventArgs e)

{ treeView1.Indent = 20;

treeView1.Nodes.Add("哺乳动物");

treeView1.Nodes[0].Nodes.Add("豹子");

treeView1.Nodes[0].Nodes.Add("老虎");

treeView1.Nodes[0].Nodes.Add("北极熊");

treeView1.Nodes[0].Nodes.Add("狼");

treeView1.Nodes[0].Nodes.Add("大象");

treeView1.Nodes[0].Nodes.Add("犀牛");

treeView1.Nodes.Add("鱼类");

treeView1.Nodes[1].Nodes.Add("鲨鱼");

treeView1.Nodes[1].Nodes.Add("热带鱼");

treeView1.Nodes[1].Nodes.Add("金鱼");

treeView1.Nodes.Add("鸟类");

treeView1.Nodes[2].Nodes.Add("天鹅");

treeView1.Nodes[2].Nodes.Add("猫头鹰");

treeView1.Nodes[2].Nodes.Add("翠鸟");

}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {string s;

s = e.Node.Text;

listView1.Items.Clear();

https://www.doczj.com/doc/5a16423064.html,rgeImageList = imageList1;

listView1.SmallImageList = imageList2;

switch(s)

{ case"哺乳动物":

listView1.Items.Add("豹子", 0);

listView1.Items.Add("老虎", 0);

listView1.Items.Add("北极熊", 0);

listView1.Items.Add("狼", 0);

listView1.Items.Add("大象", 0);

listView1.Items.Add("犀牛", 0); break;

case"鱼类":

listView1.Items.Add("鲨鱼", 0);

listView1.Items.Add("热带鱼", 0);

listView1.Items.Add("金鱼", 0); break;

case"鸟类":

listView1.Items.Add("天鹅", 0);

listView1.Items.Add("猫头鹰", 0);

listView1.Items.Add("翠鸟", 0); break;

}}

相关主题
文本预览
相关文档 最新文档