C#课后习题答案.docx
《C#课后习题答案.docx》由会员分享,可在线阅读,更多相关《C#课后习题答案.docx(10页珍藏版)》请在冰豆网上搜索。
C#课后习题答案
1、编写程序(输入一个华氏温度,输出一个摄氏温度。
公式C=(F-32)*5/9)
staticvoidMain(string[]args)
{
Console.WriteLine("请输入华氏温度:
");
doublef=double.Parse(Console.ReadLine());
doublec=(f-32)*5/9;
Console.WriteLine("对应摄氏温度为:
"+c);
Console.ReadLine();
}
2、编写一个C#控制台应用程序,对于输入的正整数n,计算1!
+2!
+3!
+…+n!
的值并输出结果。
publicintfn(intn)
intsum=0,m=1;
for(inti=1;i<=n;i++)
m=m*i;
sum+=m;
returnsum;
3、编写程序,输入一个整数,将其各位数字颠倒顺序后输出
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceConsoleApplication1
classProgram
intt=0;
try
t=int.Parse(Console.ReadLine());
catch(Exceptionee)
Console.WriteLine(ee.Message);
return;
strings=t.ToString();
for(inti=s.Length-1;i>=0;i--)
Console.Write(s[i]);
Console.Read();
4、C#写一个输入三个整数,按大到小顺序输出的小程序
int[]arr=newint[3];
for(inti=0;i{Console.WriteLine("请输入第"+(i+1)+"个数");arr[i]=Convert.ToInt32(Console.ReadLine());}for(inti=0;i{for(intj=0;j{if(arr[j]{inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}for(inti=0;i{Console.WriteLine(arr[i]);}5、编写程序,输入年份和月份,输出该年该月的天数inty,m,d;//定义三个变量,分别用来接收年月日intsum=0,count=0;//sum统计总天数,count储存天数inttotal=0;//储存输入月份后有多少天y=Convert.ToInt32(txtYear.Text);//强制类型转换m=Convert.ToInt32(txtMonth.Text);//d=Convert.ToInt32(txtDays.Text);//switch(m)//输入月份后获得该月天数{case1:case3:case5:case7:case8:case10:case12:total=31;break;case4:case6:case9:case11:total=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){total=29;}else{total=28;}break;}if(d>total)//如果输入2010229就会出错{txtXianShi.Text="该天不存在";}else{if(y>=1&&y<=9999){if(m>=1&&m<=12){for(inti=1;i<=m;i++)//循环累加天数{switch(i){case1:case3:case5:case7:case8:case10:case12:count=31;break;case4:case6:case9:case11:count=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){count=29;}else{count=28;}break;}sum=sum+count;//循环一次累加一个月的天数}txtXianShi.Text=sum.ToString();//类型转换后输出}else{txtXianShi.Text="你输入的年份不正确!请重新输入!";}}else{txtXianShi.Text="你输入的月份不正确!请重新输入!";}}6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和#includeintmain(){doublea=2.0,b=1.0;doublesum=0.0;inti=0;while(i<20){sum+=a/b;doubletemp=b;b=a;a=a+temp;i++;}printf("sum=%lf\n",sum);return0;}7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔#includemain(){intchicken,rabbit;intlegs=90,sum=30;rabbit=(90-30*2)/2;chicken=30-rabbit;printf("%d只兔子,%d只鸡",rabbit,chicken);}8、编辑程序,求1~100之间的所有质数usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication10{classProgram{staticvoidMain(string[]args){intmin,max,i,j,n;boolflag;Console.WriteLine("素数\n");Console.WriteLine("输入范围的下限(不小于2)");min=Convert.ToInt16(Console.ReadLine());Console.WriteLine("输入范围的上限");max=Convert.ToInt16(Console.ReadLine());n=0;//初始化素数的个数为0for(i=min;i<=max;i++)//i为min到max之间的所有自然数{flag=true;//设置为真for(j=2;j<=Math.Sqrt(i);j++){if(i%j==0)//如果i能被j整除{flag=false;//设置标记为假break;//退出内循环}}if(!flag)//如果标记为假{continue;}//退出本次循环Console.Write("{0,6}",i);//输出素数n++;/素数递增if(n%10==0)//没输出十个素数换一次行{Console.WriteLine();}}Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);Console.ReadLine();}}}9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数(1)在设计中拉十个RadioButton控件和一个Button控件。双击Button控件,写入以下代码!protectedvoidButton1_Click(objectsender,EventArgse){intn=0;inti=0;if(RadioButton1.Checked==true){n=n+10;i++;}if(RadioButton2.Checked==true){n=n+10;i++;}........if(RadioButton10.Checked==true){n=n+10;i++;}}你将n打出来它就是你要的分数,而i就是正确答案的个数(2)Gridview中的RadioButton在Gridview中放了一个radiobutton组GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>GridView>LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">Label>SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
Console.WriteLine("请输入第"+(i+1)+"个数");
arr[i]=Convert.ToInt32(Console.ReadLine());
for(inti=0;i{for(intj=0;j{if(arr[j]{inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}for(inti=0;i{Console.WriteLine(arr[i]);}5、编写程序,输入年份和月份,输出该年该月的天数inty,m,d;//定义三个变量,分别用来接收年月日intsum=0,count=0;//sum统计总天数,count储存天数inttotal=0;//储存输入月份后有多少天y=Convert.ToInt32(txtYear.Text);//强制类型转换m=Convert.ToInt32(txtMonth.Text);//d=Convert.ToInt32(txtDays.Text);//switch(m)//输入月份后获得该月天数{case1:case3:case5:case7:case8:case10:case12:total=31;break;case4:case6:case9:case11:total=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){total=29;}else{total=28;}break;}if(d>total)//如果输入2010229就会出错{txtXianShi.Text="该天不存在";}else{if(y>=1&&y<=9999){if(m>=1&&m<=12){for(inti=1;i<=m;i++)//循环累加天数{switch(i){case1:case3:case5:case7:case8:case10:case12:count=31;break;case4:case6:case9:case11:count=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){count=29;}else{count=28;}break;}sum=sum+count;//循环一次累加一个月的天数}txtXianShi.Text=sum.ToString();//类型转换后输出}else{txtXianShi.Text="你输入的年份不正确!请重新输入!";}}else{txtXianShi.Text="你输入的月份不正确!请重新输入!";}}6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和#includeintmain(){doublea=2.0,b=1.0;doublesum=0.0;inti=0;while(i<20){sum+=a/b;doubletemp=b;b=a;a=a+temp;i++;}printf("sum=%lf\n",sum);return0;}7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔#includemain(){intchicken,rabbit;intlegs=90,sum=30;rabbit=(90-30*2)/2;chicken=30-rabbit;printf("%d只兔子,%d只鸡",rabbit,chicken);}8、编辑程序,求1~100之间的所有质数usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication10{classProgram{staticvoidMain(string[]args){intmin,max,i,j,n;boolflag;Console.WriteLine("素数\n");Console.WriteLine("输入范围的下限(不小于2)");min=Convert.ToInt16(Console.ReadLine());Console.WriteLine("输入范围的上限");max=Convert.ToInt16(Console.ReadLine());n=0;//初始化素数的个数为0for(i=min;i<=max;i++)//i为min到max之间的所有自然数{flag=true;//设置为真for(j=2;j<=Math.Sqrt(i);j++){if(i%j==0)//如果i能被j整除{flag=false;//设置标记为假break;//退出内循环}}if(!flag)//如果标记为假{continue;}//退出本次循环Console.Write("{0,6}",i);//输出素数n++;/素数递增if(n%10==0)//没输出十个素数换一次行{Console.WriteLine();}}Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);Console.ReadLine();}}}9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数(1)在设计中拉十个RadioButton控件和一个Button控件。双击Button控件,写入以下代码!protectedvoidButton1_Click(objectsender,EventArgse){intn=0;inti=0;if(RadioButton1.Checked==true){n=n+10;i++;}if(RadioButton2.Checked==true){n=n+10;i++;}........if(RadioButton10.Checked==true){n=n+10;i++;}}你将n打出来它就是你要的分数,而i就是正确答案的个数(2)Gridview中的RadioButton在Gridview中放了一个radiobutton组GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>GridView>LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">Label>SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
for(intj=0;j{if(arr[j]{inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}for(inti=0;i{Console.WriteLine(arr[i]);}5、编写程序,输入年份和月份,输出该年该月的天数inty,m,d;//定义三个变量,分别用来接收年月日intsum=0,count=0;//sum统计总天数,count储存天数inttotal=0;//储存输入月份后有多少天y=Convert.ToInt32(txtYear.Text);//强制类型转换m=Convert.ToInt32(txtMonth.Text);//d=Convert.ToInt32(txtDays.Text);//switch(m)//输入月份后获得该月天数{case1:case3:case5:case7:case8:case10:case12:total=31;break;case4:case6:case9:case11:total=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){total=29;}else{total=28;}break;}if(d>total)//如果输入2010229就会出错{txtXianShi.Text="该天不存在";}else{if(y>=1&&y<=9999){if(m>=1&&m<=12){for(inti=1;i<=m;i++)//循环累加天数{switch(i){case1:case3:case5:case7:case8:case10:case12:count=31;break;case4:case6:case9:case11:count=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){count=29;}else{count=28;}break;}sum=sum+count;//循环一次累加一个月的天数}txtXianShi.Text=sum.ToString();//类型转换后输出}else{txtXianShi.Text="你输入的年份不正确!请重新输入!";}}else{txtXianShi.Text="你输入的月份不正确!请重新输入!";}}6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和#includeintmain(){doublea=2.0,b=1.0;doublesum=0.0;inti=0;while(i<20){sum+=a/b;doubletemp=b;b=a;a=a+temp;i++;}printf("sum=%lf\n",sum);return0;}7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔#includemain(){intchicken,rabbit;intlegs=90,sum=30;rabbit=(90-30*2)/2;chicken=30-rabbit;printf("%d只兔子,%d只鸡",rabbit,chicken);}8、编辑程序,求1~100之间的所有质数usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication10{classProgram{staticvoidMain(string[]args){intmin,max,i,j,n;boolflag;Console.WriteLine("素数\n");Console.WriteLine("输入范围的下限(不小于2)");min=Convert.ToInt16(Console.ReadLine());Console.WriteLine("输入范围的上限");max=Convert.ToInt16(Console.ReadLine());n=0;//初始化素数的个数为0for(i=min;i<=max;i++)//i为min到max之间的所有自然数{flag=true;//设置为真for(j=2;j<=Math.Sqrt(i);j++){if(i%j==0)//如果i能被j整除{flag=false;//设置标记为假break;//退出内循环}}if(!flag)//如果标记为假{continue;}//退出本次循环Console.Write("{0,6}",i);//输出素数n++;/素数递增if(n%10==0)//没输出十个素数换一次行{Console.WriteLine();}}Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);Console.ReadLine();}}}9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数(1)在设计中拉十个RadioButton控件和一个Button控件。双击Button控件,写入以下代码!protectedvoidButton1_Click(objectsender,EventArgse){intn=0;inti=0;if(RadioButton1.Checked==true){n=n+10;i++;}if(RadioButton2.Checked==true){n=n+10;i++;}........if(RadioButton10.Checked==true){n=n+10;i++;}}你将n打出来它就是你要的分数,而i就是正确答案的个数(2)Gridview中的RadioButton在Gridview中放了一个radiobutton组GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>GridView>LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">Label>SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
if(arr[j]{inttemp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}for(inti=0;i{Console.WriteLine(arr[i]);}5、编写程序,输入年份和月份,输出该年该月的天数inty,m,d;//定义三个变量,分别用来接收年月日intsum=0,count=0;//sum统计总天数,count储存天数inttotal=0;//储存输入月份后有多少天y=Convert.ToInt32(txtYear.Text);//强制类型转换m=Convert.ToInt32(txtMonth.Text);//d=Convert.ToInt32(txtDays.Text);//switch(m)//输入月份后获得该月天数{case1:case3:case5:case7:case8:case10:case12:total=31;break;case4:case6:case9:case11:total=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){total=29;}else{total=28;}break;}if(d>total)//如果输入2010229就会出错{txtXianShi.Text="该天不存在";}else{if(y>=1&&y<=9999){if(m>=1&&m<=12){for(inti=1;i<=m;i++)//循环累加天数{switch(i){case1:case3:case5:case7:case8:case10:case12:count=31;break;case4:case6:case9:case11:count=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){count=29;}else{count=28;}break;}sum=sum+count;//循环一次累加一个月的天数}txtXianShi.Text=sum.ToString();//类型转换后输出}else{txtXianShi.Text="你输入的年份不正确!请重新输入!";}}else{txtXianShi.Text="你输入的月份不正确!请重新输入!";}}6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和#includeintmain(){doublea=2.0,b=1.0;doublesum=0.0;inti=0;while(i<20){sum+=a/b;doubletemp=b;b=a;a=a+temp;i++;}printf("sum=%lf\n",sum);return0;}7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔#includemain(){intchicken,rabbit;intlegs=90,sum=30;rabbit=(90-30*2)/2;chicken=30-rabbit;printf("%d只兔子,%d只鸡",rabbit,chicken);}8、编辑程序,求1~100之间的所有质数usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication10{classProgram{staticvoidMain(string[]args){intmin,max,i,j,n;boolflag;Console.WriteLine("素数\n");Console.WriteLine("输入范围的下限(不小于2)");min=Convert.ToInt16(Console.ReadLine());Console.WriteLine("输入范围的上限");max=Convert.ToInt16(Console.ReadLine());n=0;//初始化素数的个数为0for(i=min;i<=max;i++)//i为min到max之间的所有自然数{flag=true;//设置为真for(j=2;j<=Math.Sqrt(i);j++){if(i%j==0)//如果i能被j整除{flag=false;//设置标记为假break;//退出内循环}}if(!flag)//如果标记为假{continue;}//退出本次循环Console.Write("{0,6}",i);//输出素数n++;/素数递增if(n%10==0)//没输出十个素数换一次行{Console.WriteLine();}}Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);Console.ReadLine();}}}9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数(1)在设计中拉十个RadioButton控件和一个Button控件。双击Button控件,写入以下代码!protectedvoidButton1_Click(objectsender,EventArgse){intn=0;inti=0;if(RadioButton1.Checked==true){n=n+10;i++;}if(RadioButton2.Checked==true){n=n+10;i++;}........if(RadioButton10.Checked==true){n=n+10;i++;}}你将n打出来它就是你要的分数,而i就是正确答案的个数(2)Gridview中的RadioButton在Gridview中放了一个radiobutton组GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>GridView>LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">Label>SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
inttemp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
for(inti=0;i{Console.WriteLine(arr[i]);}5、编写程序,输入年份和月份,输出该年该月的天数inty,m,d;//定义三个变量,分别用来接收年月日intsum=0,count=0;//sum统计总天数,count储存天数inttotal=0;//储存输入月份后有多少天y=Convert.ToInt32(txtYear.Text);//强制类型转换m=Convert.ToInt32(txtMonth.Text);//d=Convert.ToInt32(txtDays.Text);//switch(m)//输入月份后获得该月天数{case1:case3:case5:case7:case8:case10:case12:total=31;break;case4:case6:case9:case11:total=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){total=29;}else{total=28;}break;}if(d>total)//如果输入2010229就会出错{txtXianShi.Text="该天不存在";}else{if(y>=1&&y<=9999){if(m>=1&&m<=12){for(inti=1;i<=m;i++)//循环累加天数{switch(i){case1:case3:case5:case7:case8:case10:case12:count=31;break;case4:case6:case9:case11:count=30;break;case2:if((y%100!=0)&&(y%4==0)||(y%400==0)){count=29;}else{count=28;}break;}sum=sum+count;//循环一次累加一个月的天数}txtXianShi.Text=sum.ToString();//类型转换后输出}else{txtXianShi.Text="你输入的年份不正确!请重新输入!";}}else{txtXianShi.Text="你输入的月份不正确!请重新输入!";}}6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和#includeintmain(){doublea=2.0,b=1.0;doublesum=0.0;inti=0;while(i<20){sum+=a/b;doubletemp=b;b=a;a=a+temp;i++;}printf("sum=%lf\n",sum);return0;}7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔#includemain(){intchicken,rabbit;intlegs=90,sum=30;rabbit=(90-30*2)/2;chicken=30-rabbit;printf("%d只兔子,%d只鸡",rabbit,chicken);}8、编辑程序,求1~100之间的所有质数usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceConsoleApplication10{classProgram{staticvoidMain(string[]args){intmin,max,i,j,n;boolflag;Console.WriteLine("素数\n");Console.WriteLine("输入范围的下限(不小于2)");min=Convert.ToInt16(Console.ReadLine());Console.WriteLine("输入范围的上限");max=Convert.ToInt16(Console.ReadLine());n=0;//初始化素数的个数为0for(i=min;i<=max;i++)//i为min到max之间的所有自然数{flag=true;//设置为真for(j=2;j<=Math.Sqrt(i);j++){if(i%j==0)//如果i能被j整除{flag=false;//设置标记为假break;//退出内循环}}if(!flag)//如果标记为假{continue;}//退出本次循环Console.Write("{0,6}",i);//输出素数n++;/素数递增if(n%10==0)//没输出十个素数换一次行{Console.WriteLine();}}Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);Console.ReadLine();}}}9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数(1)在设计中拉十个RadioButton控件和一个Button控件。双击Button控件,写入以下代码!protectedvoidButton1_Click(objectsender,EventArgse){intn=0;inti=0;if(RadioButton1.Checked==true){n=n+10;i++;}if(RadioButton2.Checked==true){n=n+10;i++;}........if(RadioButton10.Checked==true){n=n+10;i++;}}你将n打出来它就是你要的分数,而i就是正确答案的个数(2)Gridview中的RadioButton在Gridview中放了一个radiobutton组GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>GridView>LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">Label>SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
Console.WriteLine(arr[i]);
5、编写程序,输入年份和月份,输出该年该月的天数
inty,m,d;//定义三个变量,分别用来接收年月日
intsum=0,count=0;//sum统计总天数,count储存天数
inttotal=0;//储存输入月份后有多少天
y=Convert.ToInt32(txtYear.Text);//强制类型转换
m=Convert.ToInt32(txtMonth.Text);//
d=Convert.ToInt32(txtDays.Text);//
switch(m)//输入月份后获得该月天数
case1:
case3:
case5:
case7:
case8:
case10:
case12:
total=31;
break;
case4:
case6:
case9:
case11:
total=30;
case2:
if((y%100!
=0)&&(y%4==0)||(y%400==0))
total=29;
else
total=28;
if(d>total)//如果输入2010229就会出错
txtXianShi.Text="该天不存在";
if(y>=1&&y<=9999)
if(m>=1&&m<=12)
for(inti=1;i<=m;i++)//循环累加天数
switch(i)
count=31;
count=30;
count=29;
count=28;
sum=sum+count;//循环一次累加一个月的天数
txtXianShi.Text=sum.ToString();//类型转换后输出
txtXianShi.Text="你输入的年份不正确!
请重新输入!
";
txtXianShi.Text="你输入的月份不正确!
6、求公式1/2+3/2+5/3+8/5+13/8+21/13+.....中前20项的和
#include
intmain()
doublea=2.0,b=1.0;
doublesum=0.0;
inti=0;
while(i<20)
sum+=a/b;
doubletemp=b;
b=a;
a=a+temp;
i++;
printf("sum=%lf\n",sum);
return0;
7、鸡兔同笼,鸡兔共30只,脚90只,计算共多少只鸡多少只兔
main(){
intchicken,rabbit;
intlegs=90,sum=30;
rabbit=(90-30*2)/2;
chicken=30-rabbit;
printf("%d只兔子,%d只鸡",rabbit,chicken);
8、编辑程序,求1~100之间的所有质数
namespaceConsoleApplication10
intmin,max,i,j,n;
boolflag;
Console.WriteLine("素数\n");
Console.WriteLine("输入范围的下限(不小于2)");
min=Convert.ToInt16(Console.ReadLine());
Console.WriteLine("输入范围的上限");
max=Convert.ToInt16(Console.ReadLine());
n=0;//初始化素数的个数为0
for(i=min;i<=max;i++)//i为min到max之间的所有自然数
flag=true;//设置为真
for(j=2;j<=Math.Sqrt(i);j++)
if(i%j==0)//如果i能被j整除
flag=false;//设置标记为假
break;//退出内循环
if(!
flag)//如果标记为假
{continue;}//退出本次循环
Console.Write("{0,6}",i);//输出素数
n++;/素数递增
if(n%10==0)//没输出十个素数换一次行
Console.WriteLine();
Console.WriteLine("\n{0}到{1}之间的素数共有{2}",min,max,n);
9、编写程序,显示10个单项选择题,用户选择答案并提交后给出分数
(1)在设计中拉十个RadioButton控件和一个Button控件。
双击Button控件,写入以下代码!
protectedvoidButton1_Click(objectsender,EventArgse)
intn=0;
if(RadioButton1.Checked==true)
n=n+10;
if(RadioButton2.Checked==true)
........
if(RadioButton10.Checked==true)
你将n打出来它就是你要的分数,而i就是正确答案的个数
(2)Gridview中的RadioButton
在Gridview中放了一个radiobutton组
GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>TemplateField>
GridViewID="GridView1"runat="server"AutoGenerateColumns="False"SelectedIndex="0"Width="343px"OnSelectedIndexChanged="papersubmit_Click">
TemplateField>Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>Label>Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>Label>RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>RadioButton>
TemplateField>
Labelid=squerunat=serverText='<%#Eval("Ques_Squc")%>'>
Label>
Labelid=keyrunat=serverText='<%#Eval("Answe_Key")%>'Visible=false>
RadioButtonid=RadioButton1runat="server"Text='<%#Eval("Answer_A")%>'GroupName='<%#Eval("Ques_Squc")%>'>
RadioButton>
RadioButtonid=RadioButton2runat="server"Text='<%#Eval("Answer_B")%>'GroupName='<%#Eval("Ques_Squc")%>'>
RadioButtonid=RadioButton3runat="server"Text='<%#Eval("Answer_C")%>'GroupName='<%#Eval("Ques_Squc")%>'>
RadioButtonid=RadioButton4runat="server"Text='<%#Eval("Answe_D")%>'GroupName='<%#Eval("Ques_Squc")%>'>
GridView>
LabelID="Lb_Score"runat="server"Enabled="False"Text="Label"Width="144px"ForeColor="Red"Visible="False">
SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:paperconn%>"SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">ParameterName="File_ID"Type="Int32"/>SqlDataSource>ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
SqlDataSourceID="paperconn"runat="server"ConnectionString="<%$ConnectionStrings:
paperconn%>"
SelectCommand="SELECT[Answer_A],[Answer_B],[Answer_C],[Answe_D],[Answe_Key],[Ques_Squc],[Ques_Title]FROM[Ques_Info]WHERE([File_ID]=@File_ID)ORDERBY[Ques_Squc]">
ParameterName="File_ID"Type="Int32"/>
SqlDataSource>
ButtonID="papersubmit"runat="server"OnClick="papersubmit_Click"Text="交卷"/>
结果点击"交卷"按钮后执行protectedvoidpapersubmit_Click(objectsender,EventArgse)
intscore=0;
foreach(GridViewRowdrinGridView1.Rows)
stringstr="";
stringkey="";
key=((Label)GridView1.Rows[0].FindControl("key")).Text;
intradio_pre=GridView1.Controls.Count;
if(((RadioButton)dr.FindControl("RadioButton1")).Checked)
str="A";
elseif(((RadioButton)dr.FindControl("RadioButton2")).Checked)
str="B";
elseif(((RadioButton)dr.FindControl("RadioButton3")).Checked)
str="C";
elseif(((RadioButton)dr.FindControl("RadioButton4")).Checked)
str="D";
if(str==key)
score=score+1;
Lb_Score.Visible=true;
Lb_Score.Text="你答对了"+score.ToString()+"个题";
10、编写程序,让用户输入5个学生的姓名,年龄和住址等信息,在提交后现实
<%
dname=request("dname")
dage=request("dage")
dadd=request("dadd")
%>
姓名:
年龄:
住址:
<%=dname%>
<%=dage%>
地址:
<%=dadd%>
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1