实验二C.docx
- 文档编号:23782778
- 上传时间:2023-05-20
- 格式:DOCX
- 页数:14
- 大小:1.10MB
实验二C.docx
《实验二C.docx》由会员分享,可在线阅读,更多相关《实验二C.docx(14页珍藏版)》请在冰豆网上搜索。
实验二C
实验二C#语言实验及WebForm实验
一、实验目的
(1)掌握在VisualStudio环境下C#控制台程序的建立、编辑、编译和执行过程;
(2)基本掌握C#程序的最基本框架结构、基本特征;
(3)掌握发现语法错误、逻辑错误的方法以及排除简单错误的操作技能;
(4)练习C#中变量声明和使用方法、类型转换方法、运算符和表达式的使用方法;
(5)掌握几种控制语句的使用方法;
(6)初步掌握C#语言面向对象的基本基本编程方法;
(7)基本掌握C#语言数组和字符串的使用与操作;
(8)掌握项目的创建方法;
(9)掌握常用控件的设置与处理;
(10)掌握.NET程序的调试运行;
二、实验环境
PC机,ASP.NET开发环境及相关软件。
三、实验内容及过程
1、创建并运行控制台应用程序
(1)启动VisualStudio。
(2)在“文件”菜单上指向“新建”,再单击“项目”
(3)在“模板类别”窗格中,展开“VisualC#”,然后单击“Windows”。
(4)在“模板”窗格中单击“控制台应用程序”。
(5)在“名称”字段中键入项目的名称。
(6)单击“确定”。
(7)运行VisualS,观察并熟悉集成开发环境;
《1》启动VisualStudio2005
“开始”——>“所有程序”——>“MicrosoftVisualStudio2005”,单击“MicrosoftVisualStudio2005”
《2》认识VisualStudio2005
打开VisualStudio2005之后,单击“文件”——>“新建网站”,或者单击起始页上的“创建网站”;
(8)程序代码
新项目出现在“解决方案资源管理器”中。
a如果Program.cs不是在“代码编辑器”中打开,则在“解决方案资源管理器”中右击Program.cs,然后单击“查看代码”。
b用下面的代码替换Program.cs的内容。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceConsoleApplication1
{
classProgram
{
staticvoidMain(string[]args)
{
Console.WriteLine("Pleaseenteryourname:
");
Console.ReadLine();
Console.WriteLine("Welcometoyou!
");
}
}
}
c、按F5运行项目。
结果如下:
d、基础实验
问题:
计算圆面积,圆面积公式为Area=∏×R2,这种表示符号,在计算机中只能表示为Area=PI*R*R,在本例中,我们希望用户输入圆的半径,但是用户的输入只能是字符串,因此,这里我们要用到数制的转换,注意数制的转换方法
1)新建一个名为CircleArea的控制台应用程序
2)输入如下代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceCircleArea
{
classProgram
{
staticvoidMain(string[]args)
{
//定义常量
constfloatPI=3.1415926f;
Console.Write("请输入圆的半径:
");
//获取用户的输入
stringinput=Console.ReadLine();
//将字符串转化为浮点数
floatradius=float.Parse(input);
//计算圆面积
floatcirclArea=radius*radius*PI;
//输出结果
Console.WriteLine("圆面积为:
{0}",circlArea);
}
}
}
运行结果如下:
e、循环、条件语句实验
问题:
实现n的阶乘
1)新建一个名为Factorial的控制台应用程序
2)输入如下代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceFactorial
{
classProgram
{
staticvoidMain(string[]args)
{
Console.Write("请输入一个正整数:
");
//接收用户输入
stringinput=Console.ReadLine();
//转化为整数
intn=int.Parse(input);
//以上代码可以合写为
//intn=int.Parse(Console.ReadLine());
inti=1;
intresult=1;
while(i<=n)
{
result=result*i;//或者写成result*=i;
i++;
}
Console.WriteLine("{0}的阶乘为{1}",n,result);
}
}
}
运行结果如下:
(2)问题:
打印9*9乘法表
1)新建一个名为MutiTable的控制台应用程序
2)输入如下代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceMultiTable
{
classProgram
{
staticvoidMain(string[]args)
{
//9行
for(inti=1;i<=9;i++)
{
//每行最多i列
for(intj=1;j<=i;j++)
{
Console.Write("{0}*{1}={2}\t",i,j,i*j);
}
//换行
Console.WriteLine();
}
}
}
}
f问题:
定义一个元素个数为5的整形数组,接收用户的输入,而后对该数组进行排序,输出排序后的结果。
1)建立一个名为ArrayDemo的控制台项目
2)双击Program.cs,输入如下代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceArrayDemo
{
classProgram
{
staticvoidMain(string[]args)
{
int[]number=newint[5];
for(inti=0;i<5;i++)
{
Console.Write("请输入第{0}个数:
",i+1);
stringinput=Console.ReadLine();
number[i]=int.Parse(input);
}
Array.Sort(number);
Console.WriteLine("5个数排序后的结果为:
");
for(intj=0;j<5;j++)
{
Console.Write("{0}\t",number[j]);
}
Console.WriteLine();
}
}
}
(9)、完成下列ASP.NET应用程序
1)添加Label1,TEXT属性设为“姓名”
2)添加TextBox,用来输入姓名
3)添加Label2,TEXT属性设为“性别”
4)添加DropDownliat,设置“男”,值为1;设置“女”,值为2
5)添加Button1,TEXT属性设为“确定”
6)添加Label3,TEXT属性设为“性别”
7)为Button1的单击事件添加代码
Label3.Text=TextBox1.Text;
if(DropDownList1.SelectedItem.Value=="1")
{
Label3.Visible=true;
Label3.Text+="先生,你好!
";
}
else
{
Label3.Visible=true;
Label3.Text+="女士,你好!
";
}
TextBox1.Text=string.Empty;
(7)添加Label1,TEXT属性设为“姓名”
(8)运行程序:
按Ctrl+F5组合键运行程序,观察运行结果
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验二 实验