50道C#编程题及答案(二)
21、单击“显示”按钮,在TextBox中显示所填和所选信息


private void button1_Click(object sender, EventArgs e)
{
textBox2.Text ="";
textBox2.Text = "姓名为:" + textBox1.Text + " " ;
textBox2.Text += "班级为:" + textBox3.Text + " ";
string sex = "";
if (radioButton1.Checked)
sex = "男";
else
sex = "女";
textBox2.Text += "性别为:" + sex + " ";
string hobby = "";
if (checkBox1.Checked) hobby += "旅游";
if (checkBox2.Checked) hobby += "游戏";
if (checkBox3.Checked) hobby += "逛街";
if (checkBox4.Checked) hobby += "电脑";
textBox2.Text += "爱好为:" + hobby;
}
22、编程计算如图所示圆面积的窗体,其中有两个标签控件label1和label2,两个文本框控件textBox1和textBox2,一个命令按钮控件button1。在textBox1用于输入圆半径,textBox2用于输出圆面积。计算功能由命令按钮实现。
{
textBox2.Text ="";
textBox2.Text = "姓名为:" + textBox1.Text + " " ;
textBox2.Text += "班级为:" + textBox3.Text + " ";
string sex = "";
if (radioButton1.Checked)
sex = "男";
else
sex = "女";
textBox2.Text += "性别为:" + sex + " ";
string hobby = "";
if (checkBox1.Checked) hobby += "旅游";
if (checkBox2.Checked) hobby += "游戏";
if (checkBox3.Checked) hobby += "逛街";
if (checkBox4.Checked) hobby += "电脑";
textBox2.Text += "爱好为:" + hobby;
}
22、编程计算如图所示圆面积的窗体,其中有两个标签控件label1和label2,两个文本框控件textBox1和textBox2,一个命令按钮控件button1。在textBox1用于输入圆半径,textBox2用于输出圆面积。计算功能由命令按钮实现。

{
double r = Convert.ToDouble(textBox1.Text);
double area = Math.PI * r * r;
textBox2.Text = area.ToString();
}
23、键盘输入十个数值,统计输出大于等于平均数的数值。
static void Main(string[] args)
{
int[] a = new int[10];
int sum = 0 ;
double avg;
for (int i = 0; i < 10; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
sum += a[i];
}
avg = sum / 10.0;
Console.WriteLine("平均值是{0}", avg);
Console.WriteLine("大于平均值的数如下:");
for (int i = 0; i < 10; i++)
if (a[i] > avg)
Console.WriteLine(a[i]);
Console.ReadLine();
}
24、已知有5个元素的数组a,请用冒泡排序法将其排序。
static void Main(string[] args)
{
int[] a ={ 1, 4, 5, 2, 0 };
int t;
for(int i=0;i<4;i++)
for (int j = 0; j < 4 -i; j++)
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
foreach (int k in a)
Console.WriteLine(k);
Console.ReadLine();
}
25、输出1-100 之间不能被6整除的全部数字。
static void Main(string[] args)
{
for(int i=1;i<=100;i++)
if ( i % 6 != 0)
{
Console.Write(i+"t");
}
Console.ReadLine();
}
24、编程输出1-100之间能够被3整除但不能被5整除的数,并统计共有多少个这样的数。
static void Main(string[] args)
{
int count = 0;
for(int i=1;i<=100;i++)
if (i % 3 == 0 && i % 5 != 0)
{
Console.Write(i+"t");
count++;
}
Console.WriteLine();
Console.WriteLine("共有{0}个", count);
Console.ReadLine();
}
26、编程求100以内能够被7整除的最大自然数。
static void Main(string[] args)
{
for(int i=100;i>0;i--)
if (i % 7 == 0)
{
Console.WriteLine(i);
break;
}
Console.ReadLine();
}
27、求斐波那契(Fibonacci)数列的第 10 项,已知该数列的前两项都为 1,即 F(1)=1,F(2)=1; 而后各项满足:F(n)=F(n-1)+F(n-2)。
static void Main(string[] args)
{
Console.WriteLine("第10项是" + f(10));
Console.ReadLine();
}
static int f(int n)
{
int r;
if (n == 1 || n == 2)
r= 1;
else
r= f(n - 1) + f(n - 2);
return r;
}
27、设计一个程序使用person类,其类中包含如下信息:姓名:name;血型:blood;创建一个person对象p如下:person p=new person(“江涛”,“AB”);然后,使用printname方法将姓名显示出来。使用printblood方法将血型显示出来。
class Program
{
static void Main(string[] args)
{
person p = new person("江涛", "AB");
p.printname();
p.printblood();
Console.ReadLine();
}
}
class person
{
string name, blood;
public person(string name, string blood)
{
this.name = name;
this.blood = blood;
}
public void printname()
{
Console.WriteLine("名字是:" + name);
}
public void printblood()
{
Console.WriteLine("血型是:" + blood);
}
}
28、创建一个Circle类,两个方法分别求圆的面积和周长。并在主函数中调用。
class Program
{
static void Main(string[] args)
{
Test t = new Test(2);
Console.WriteLine("周长是" + t.GetCircumference());
Console.WriteLine("面积是" + t.GetArea());
Console.ReadLine();
}
}
public class Test
{
double r;
public Test(double r)
{
this.r = r;
}
public double GetCircumference()
{
return Math.PI * r * 2;
}
public double GetArea()
{
return Math.PI * r * r;
}
}
29、编写Test类,包含GetCircumference和GetArea方法,分别求矩形形的周长和面积,Main中根据键盘输入的长和宽,调用两个方法,最终显示结果。
class Program
{
static void Main(string[] args)
{
Test t = new Test(2, 3);
Console.WriteLine("周长是" + t.GetCircumference());
Console.WriteLine("面积是" + t.GetArea());
Console.ReadLine();
}
}
public class Test
{
double width, length;
public Test(double w, double l)
{
width = w;
length = l;
}
public double GetCircumference()
{
return (width + length) * 2;
}
public double GetArea()
{
return width * length;
}
}
30、编写一个信息类information。使用shezhi方法设置会员的姓名、年龄、学校信息。使用xianshi方法将会员的姓名、年龄、学校信息显示出来。
class Program
{
static void Main(string[] args)
{
information f = new information();
f.shezhi("张三", "沈阳理工", 20);
f.xianshi();
Console.ReadLine();
}
}
class information
{
string name, school;
int age;
public void shezhi(string name, string school, int age)
{
this.name = name;
this.school = school;
this.age = age;
}
public void xianshi()
{
Console.WriteLine("姓名是:{0}", name);
Console.WriteLine("学校是:{0}", school);
Console.WriteLine("年龄是:{0}", age);
}
}
31、编程计算如图所示圆面积的窗体,其中有两个标签控件label1和label2,两个文本框控件textBox1和textBox2,一个命令按钮控件button1。在textBox1用于输入圆半径,textBox2用于输出圆面积。计算功能由命令按钮实现。

{
double r = Convert.ToDouble(textBox1.Text);
double area = Math.PI * r * r;
textBox2.Text = area.ToString();
}
32、编写一个程序,输入梯形的上底,下底和高,输出梯形的面积。要求编写成Window应用程序。

{
double l = Convert.ToDouble(textBox1.Text);
double w = Convert.ToDouble(textBox2.Text);
double h = Convert.ToDouble(textBox3.Text);
double area = (l + w) * h / 2.0;
label4.Text = "梯形面积是" + area.ToString();
}
33、3个RadioButton,文本中分别显示软件工程、计算机科学与技术和网络工程。对3个RadioButton任意选定,一个Button按钮单击按钮后弹出消息框,显示被选中信息。

{
string msg="";
if (radioButton1.Checked)
msg += "软件工程";
else if (radioButton2.Checked)
msg += "计算机科学与技术";
else
msg += "网络工程";
MessageBox.Show(msg +"专业被选择");
}
34、4个CheckBox控件,文本中分别显示C#程序设计、SQL Server、.NET Framework和ADO.NET,对4个CheckBox任意选定,单击按钮后弹出消息框,显示被选中信息。

{
string msg = "";
if (checkBox1.Checked)
msg += "C#";
if (checkBox2.Checked)
msg += "SQL Server";
if (checkBox3.Checked)
msg += ".NET Framework";
原文链接:http://www.jxszl.com/biancheng/C/445881.html