net编程.docx
《net编程.docx》由会员分享,可在线阅读,更多相关《net编程.docx(23页珍藏版)》请在冰豆网上搜索。
net编程
1、变量a、b、c之和为:
55
inta=15,b,c,sum;
b=c=20;
sum=a+b+c;
lblShow.Text="变量a、b、c之和为:
"+sum;
2、枚举变量currentSeason的值为:
11
枚举变量nextSeason代表枚举元素:
Winter
publicpartialclassForm1:
Form
{
enumSeason{Spring=10,Summer,Autumn=20,Winter};
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
SeasoncurrentSeason,nextSeason;
currentSeason=Season.Summer;
nextSeason=(Season)21;
lblShow.Text="枚举变量currentSeason的值为:
"+(int)currentSeason;
lblShow.Text+="\n枚举变量nextSeason代表枚举元素:
"+nextSeason;
}
}
3、学生信息:
姓名:
学号:
性别:
年龄
publicpartialclassForm1:
Form
{
structStudent
{
publicintstuNo;
publicstringstuName;
publicintage;
publiccharsex;
}
publicForm1()
{
InitializeComponent();
}
privatevoidForm1_Load(objectsender,EventArgse)
{
Studentstu;
stu.stuNo=1001;
stu.stuName="乔峰";
stu.age=23;
stu.sex='男';
lblShow.Text="学生信息:
\n姓名:
"+stu.stuName;
lblShow.Text+="\n学号:
"+stu.stuNo;
lblShow.Text+="\n性别:
"+stu.sex;
lblShow.Text+="\n年龄:
"+stu.age;
}
}
4、算数运算符的应用测试
privatevoidForm1_Load(objectsender,EventArgse)
{
intnum1=5,num2=2;
inta=num1%num2;
intb=num1/num2;
lblShow.Text=num1+"%"+num2+"="+a;
lblShow.Text+="\n"+num1+"/"+num2+"="+b;
a=num1++;
b=--num2;
lblShow.Text+="\n"+"a=num1++;后num1="+num1+",a="+a;
lblShow.Text+="\n"+"b=--num2;后num2="+num2+",b="+b;
}
5、赋值运算符及隐式数据类型转换应用测试
privatevoidForm1_Load(objectsender,EventArgse)
{
inta,b=5;
charc='中';
a=c+2;
lblShow.Text="整型变量a的值为:
"+a;
doublex=42;
x/=b;
lblShow.Text+="\n浮点型变量x的值为:
"+x;
}
6、测试关系运算符和逻辑运算符
privatevoidForm1_Load(objectsender,EventArgse)
{
inta=8,b=5;
boolk;
k=a!
=b;
lblShow.Text="a!
=b;的结果是:
"+k;
k=a<=b&&a<=10;
lblShow.Text+="\na>=0&&&&a<=10;的结果是:
"+k;
k=a<=b&&++a==9;
lblShow.Text+="\na<=b&&&&++a==9;的结果是:
"+k+",a在执行后的结果是:
"+a;
k=a<=b&++a==9;
lblShow.Text+="\na<=b&&++a==9;的结果是:
"+k+",a在执行后的结果是:
"+a;
}
7、数组及其应用演示
privatevoidForm1_Load(objectsender,EventArgse)
{
int[]a={23,15,27,12,24};
int[]b=newint[5];
Array.Copy(a,b,5);
b[3]=18;
Array.Clear(a,0,5);
lblShow.Text="数组b的原始值:
"+b[0]+""+b[1]+""+b[2]+""+b[3]+""++b[4]+"\n";
Array.Sort(b);
lblShow.Text+="数组b排序后值:
"+b[0]+""+b[1]+""+b[2]+""+b[3]+""++b[4]+"\n";
Array.Reverse(b);
lblShow.Text+="反转数组b的值:
"+b[0]+""+b[1]+""+b[2]+""+b[3]+""++b[4]+"\n";
intloc=Array.IndexOf(b,18);
lblShow.Text+="18是数组b中第"+(loc+1)+"个元素";
}
8、多维数组、数组型的数组的应用展示
privatevoidForm1_Load(objectsender,EventArgse)
{
int[,]a=newint[2,3]{{1,2,3},{4,5,5}};
int[][]b=newint[2][];
b[0]=newint[3]{1,2,3};
b[1]=newint[4]{4,5,6,7};
lblShow.Text="a是二维数组,共6个数组元素,均为整数值。
\n";
lblShow.Text+="b是一维数组,共2个数组元素,均为子数组。
\n";
lblShow.Text+="a[0,0]的值为"+a[0,0];
lblShow.Text+="\nb[0][0]的值为"+b[0][0];
}
9、展示字符串及其应用
publicpartialclassForm1:
Form
{
StringBuildersb=newStringBuilder();
publicForm1()
{
InitializeComponent();
}
privatevoidbtnInsert_Click(objectsender,EventArgse)
{
sb.Append(txtSource.Text);
intpos=Convert.ToInt32(txtPos.Text);
sb.Insert(pos,txtInsert.Text,1);
txtSource.Text=sb.ToString();
}
privatevoidbtnSelect_Click(objectsender,EventArgse)
{
sb.Append(txtSource.Text);
stringstr=sb.ToString();
intloc=str.IndexOf(txtSelect.Text);
lblShow.Text="\n查找的字符串在主串中的起始索引值为"+loc;
}
}
10、摄氏度转换
doublec=Convert.ToInt32(txtCelsius.Text);
doublef=9.0/5*c+32;
txtFahrenheit.Text=f.ToString();
11、简单的储蓄存款计算器:
intmoney=Convert.ToInt32(txtMoney.Text);
intyear=Convert.ToInt32(txtYear.Text);
doublerate=Convert.ToDouble(txtRate.Text)/100;
doubleinterest=money*rate*year;
txtInterest.Text=interest.ToString();
doubletotal=money+interest;
txtTotal.Text=total.ToString();
12、5个数字排序
double[]a=newdouble[5];
inti=0;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
doubleelement=double.Parse(txtElement.Text);
a[i]=element;
txtPrior.Text+=a[i]+"";
i++;
lblNO.Text="第输入第"+(i+1)+"个元素";
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
Array.Sort(a);
txtSort.Text=a[0]+""+a[1]+""+a[2]+""+a[3]+""+a[4];
}
13、创建一个Windows应用程序,输入n,求1+2+3+…+n。
intn=Convert.ToInt32(txtNum.Text);
inti=1;
intsum=0;
while(i<=n)
{
sum+=i;
i++;
}
lblShow.Text="1+2+...+"+n+"="+sum;
14、创建一个Windows应用程序,输入n,求n!
,即1*2*3…*n.
intn=Convert.ToInt32(txtNum.Text);
inti=1;
intsum=1;
do
{
sum*=i;
i++;
}
while(i<=n);
lblShow.Text="1*2*...*"+n+"="+sum;
15、计算体重
doubleh,w,t;
h=Convert.ToDouble(txtHeight.Text);
w=Convert.ToDouble(txtWeight.Text);
t=w/(h*h);
if(t<18)
lblShow.Text="您的身材偏瘦!
";
elseif(t>=18&&t<25)
lblShow.Text="您的身材完全标准!
";
elseif(t<=25&&t<27)
lblShow.Text="您的身材偏胖!
";
else
lblShow.Text="您的身材有点肥胖!
";
16、一个百万富翁遇到一个陌生人
inti;
doublet,s1,s2;
s1=t=0.01;
s2=100000;
for(i=2;i<=30;i++)
{
t=t*2;
s1=s1+t;
s2=s2+100000;
}
lblShow.Text=String.Format("百万富翁给陌生人{0:
N2}元。
\n陌生人给百万富翁{1:
N2}元。
",s1,s2);
17、输入三角形行数,打印等腰三角形
privatevoidbtnOk_Click(objectsender,EventArgse)
{
intn=Convert.ToInt32(txtNum.Text);
StringBuildersb=newStringBuilder();
inti,j;
for(i=1;i<=n;i++)
{
sb.Append("");
}
for(j=1;j<=2*i-1;j++)
{sb.Append("*");
}
sb.Append("\n");
}
lblShow.Text=sb.ToString();
}
18、有一个函数:
输入x,输出y
函数计算(x<1)(1==10)
doubleX=Convert.ToDouble(txtX.Text);
doubley;
if(X<1)
y=X;
elseif(X>=1&&X<10)
y=2*X-1;
else
y=3*X-11;
txtY.Text=y.ToString();
19、输入联系人的姓名,电话和Email,单击“添加”扭,显示该联系人的相应信息。
stringname=txtname.Text;
stringphone=txtphone.Text;
if(phone=="")phone=null;
stringemail=txtemail.Text;
if(email=="")email=null;
AddressBooka=newAddressBook(name,phone,email);
txtmessage.Text=a.GetMessage();
}
classAddressBook
{
privatestringname;
privatestringphone;
privatestringemail;
publicAddressBook(stringname,stringphone,stringemail)
{
this.name=name;
this.phone=phone;
this.email=email;
}
publicstringName
{
get{returnname;}
}
publicstringPhone
{
get
{
if(phone==null)return"未输入";
elsereturnphone;
}
set
{
phone=value;
}
}
publicstringEmail
{
get
{
if(email==null)return"未输入";
elsereturnemail;
}
set
{
email=value;
}
}
publicstringGetMessage()
{
returnstring.Format("姓名:
{0}\n电话:
{1}\nEmail:
{2}",Name,Phone,Email);
}
20、模拟银行账号管理系统
publicpartialclasscreditNo:
Form
{
publiccreditNo()
{
InitializeComponent();
}
Accountaccount;
privatevoidbtnCrtsavingAc_Click(objectsender,EventArgse)
{
account=newAccount();
stringmessage=String.Format("创建帐户成功,用户卡号为:
{0}"
account.CreditNo);
lblshow.Text="\n"+message+"\n";
}
privatevoidbtnWithDraw_Click(objectsender,EventArgse)
{
stringmessage;
if(account==null)
message="请先创建帐户!
";
elseif(txtWithDraw.Text=="")
message="请输入取款金额";
else
{
decimalmoney=decimal.Parse(txtWithDraw.Text);
account.WithDraw(money,outmessage);
}
lblshow.Text="\n"+message+"\n";
}
privatevoidbtndeposit_Click(objectsender,EventArgse)
{
stringmessage;
if(account==null)
message="请先创建帐户!
";
elseif(txtdeposit.Text=="")
message="请输入存款金额";
else
{
decimalmoney=decimal.Parse(txtdeposit.Text);
account.Deposit(money,outmessage);
}
lblshow.Text="\n"+message+"\n";
}
privatevoidCheck_Click(objectsender,EventArgse)
{
stringmessage;
if(account==null)
message="请先创建帐户!
";
account.Check(outmessage);
lblshow.Text="\n"+message+"\n";
}
publicclassAccount
{
protectedintcreditNo;
protecteddecimalbalance;
publicAccount()
{
Randomr=newRandom();
creditNo=r.Next(100000,499999);
balance=100;
}
publicdecimalBalance
{
get
{
returnthis.balance;
}
}
publicintCreditNo
{
get
{
returnthis.creditNo;
}
}
publicvirtualboolWithDraw(decimalmoney,outstringmessage)
{
if(money<0)
{
message="操作失败!
\n输入金额不正确!
";
returnfalse;
}
elseif(balance>=money)
{
balance-=money;
message="操作成功!
\n取款"+money+"元";
returntrue;
}
else
{
message="操作失败!
\n余额不足!
";
returnfalse;
}
}
publicvirtualboolDeposit(decimalmoney,outstringmessage)
{
if(money<0)
{
message="操作失败!
\n输入金额不正确!
";
returnfalse;
}
else
{
balance+=money;
message="操作成功!
\n存款"+money+"元";
returntrue;
}
}
publicdecimalCheck(outstringmessage)
{
message="当前余额为"+balance+"元";
returnthis.balance;
}
21、两个数计算
publicdelegateintCaculate(intx,inty);
publicCaculatehandler;
inta=Convert.ToInt32(txtNum1.Text);
intb=Convert.ToInt32(txtNum2.Text);
MyMathmath=newMyMath();
handler=newCaculate(math.Add);
lblShow.Text+="两数之和:
"+handler(a,b);
handler=newCaculate(math.Multiply);
lblShow.Text+="\n两数之积为:
"+handler(a,b);
classMyMath
{
publicintAdd(intx,inty)
{returnx+y;
}
publicintMultiply(intx,inty)
{returnx*y;
}
}
22、成绩管理系统确定
privatevoidbtnYes_Click(objectsender,EventArgse)
{
stringuserName=txtName.Text;