习题参考答案2

  • 格式:doc
  • 大小:55.50 KB
  • 文档页数:7

习题参考答案 第1章 2.选择题 (1) C (2)A (3) D (4)D (5)ABCD (6)C

第2章 2.选择题 (1) A (2)C (3) AB (4)ABCD (5)B (6)B (7)C (8)B (9)D (10)A

第3章 2.选择题 (1)D (2)D (3)A (4)A (5)A (6)B (7)B (8)A

第4章 2.选择题 (1)C (2)D (3) C (4)C (5)D (6)A (7)B (8)B

第5章 2.选择题 (1)B (2)B (3)C (4)B (5)BC (6)D (7)C (8)C (9)A (10)C

第6章 2.选择题 (1)C (2)D (3)C (4)B (5)B (6)D (7)D (8)C

第7章 2.选择题 (1)D (2)C (3)A (4)B (5)C (6)A (7)C (8)A (9)B (10)A

第8章 2.选择题 (1)C (2)B (3)A (4)C (5)B (6)C (7)D (8)D

第9章 2.选择题 (1)C (2)C (3)A (4)C (5)C (6)D (7)D (8)B 代码编写题目: 1、编写一个应用程序,要求将一个圆的半径作为输入项,单击提交

按钮后在两个文本框中分别输出这个圆的周长和面积。(15分) namespace 求圆的半径 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { float r = Convert.ToSingle(textBox1 .Text.Trim()); double c = 2 * 3.14 * r; double s = 3.14 * r * r; textBox2.Text = c.ToString(); textBox3.Text = s.ToString(); } } }

2、编写程序,用while循环或do......while循环求出1~100之间的能被7整除的数,将这些数输出,计算它们的和并将和输出; public partial class Form3 : Form { public Form3() { InitializeComponent(); } static int[] array = new int[100]; int i = 0;

private void Form3_Load(object sender, EventArgs e) { int j=1; while (j <= 100) { if (j % 7 == 0) { array[i] = j; j++; i++; } else j++;

} for (int k = 0; k <= i; k++) { label1.Text += array[k] + " "; if (k==9) label1 .Text +="\n "; }

} private void button1_Click(object sender, EventArgs e) { int sum = 0; for (int k=0;k<=i;k++) sum +=array [k]; label2.Text += sum.ToString();

}

} }

3、编写程序,根据给定的年分与月份,判断该年是否闰年,并根据月份来判断是什么季节以及该月的天数。闰年的判断条件是能被400整除或者能被4整除但不能被100整除。(15分) public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int year = Convert.ToInt32(textBox1.Text.Trim()); int month = Convert.ToInt32(textBox2.Text.Trim()); string output1 = ""; string output2 = ""; if (year % 400 == 0) { output1 = "你输入的年份是闰年"; } else if (year % 4 == 0 && year % 100 == 0) { output1 = "你输入的年份是闰年";

} else { output1 = "你输入的年份不是闰年"; } //12-2 冬天 3-5 春天 6-8 夏天 9-11 秋天 //2 28天 // 1 3 5 7 8 10 12 31天 //4 6 9 11 30天 if (month >= 3 && month <= 5) { output2 = "春天"; if (month % 4 == 0) output2 = output2 + "30天"; else output2 = output2 + "31天"; } else if (month >= 6 && month <=8) { output2 = "夏天"; if (month % 6 == 0) output2 = output2 + "30天"; else output2 = output2 + "31天"; } else if (month >= 9 && month <= 11) { output2 = "秋天"; if (month % 10 == 0) output2 = output2 + "30天"; else output2 = output2 + "31天"; } else { output2 = "冬天"; if (month == 2) output2 = output2 + "30天"; else output2 = output2 + "31天"; } label3.Text += output1 + output2;

}

4、设计一个学生类,该类能够记录学生的姓名、班级、学号信息,并且能够修改这些信息,也能够输出这些信息;

class student { string Sname;

public string Sname1 { get { return Sname; } set { Sname = value; } } string Sclass;

public string Sclass1 { get { return Sclass; } set { Sclass = value; } } string Sno;

public string Sno1 { get { return Sno; } set { Sno = value; } } public student() { } public student(string namein) { Sname = namein; } public void output() { string str = "你的名字是:" + Sname + ","; str = "\n你的班级是:" + Sclass + ","; str = "\n你的学号是:" + Sno + ","; MessageBox.Show(str); }

}

??5、编写程序,定义一个Cstudent 类,其中包含私有变量

intage,strname,一个属性Age(可读可写),一个属性Name(只读不能被继承),一个根据年龄返回是否被录取的方法Permit,(录取年龄在18-20岁之间)。

6、编写程序,要求从文本框中随机输入一系列的正整数,将其保存在数组中,并显示在界面上,输入完毕后,单击排序按钮,对输入的数据进行从小到大排序输出。 public partial class Form4 : Form { public Form4() { InitializeComponent(); } static int [] burt = new int[100]; static int i = 0; private void button1_Click(object sender, EventArgs e) { int s = Convert.ToInt16(textBox1.Text.Trim()); burt[i] = s; label2.Text += burt[i] + ","; i++; textBox1.Focus(); }

private void button2_Click(object sender, EventArgs e) { Array.Sort(burt);