




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
22、编写一个冒泡法排序程序,要求在程序中能够捕获到数组下标越界的异常。class Program static void Main(string args) int arr = new int2,3,1; for(int i=0;i=0;j-) int tmp = arr0; arr0 = arr1; arr1 = tmp; try for(int i=0;i=arr.Length;i+) Console.WriteLine(arri); catch(Exception e) Console.WriteLine(e.ToString(); 23、编写一个计算器程序,要求在程序中能够捕获到被0除的异常与算术运算溢出的异常。public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) int a=0, b=0; try a = Convert.ToInt32(this.textBox1.Text); catch (OverflowException) MessageBox.Show(a超出范围, 出错); Application.Exit(); try b = Convert.ToInt32(this.textBox2.Text); catch (OverflowException) MessageBox.Show(b超出范围,出错); Application.Exit(); int c; c = a + b; this.textBox3.Text=c.ToString(); int d; d = a - b; this.textBox4.Text = d.ToString(); int g; g = a*b; this.textBox5.Text = g.ToString(); double f; try f = a / b; this.textBox6.Text = f.ToString(); catch(DivideByZeroException) MessageBox.Show(除数不能为零, 出错); Application.Exit(); 24、 编程输出1100中能被3整除但不能被5整除的数,并统计有多少个这样的数。class Program static void Main(string args) Console.WriteLine(1100中能被3整除但不能被5整除的数有:); int a = 0; for (int i = 1; i 101; i+) if (i % 3 = 0 & i % 5 != 0) Console.Write(i.ToString()+ ); a+; Console.WriteLine(); Console.WriteLine(这样的数一共有0个, a); Console.ReadLine(); 25、编程输出1000以内的所有素数。class Program static void Main(string args) Console.WriteLine(1000以内的所有素数:); int i, j; for (i = 1; i 1000; i+) for (j = 2; j i / 2) Console.Write(i.ToString()+ ); Console.ReadLine();26、编写一个程序,对输入的4个整数,求出其中最大值和最小值。class Program static void Main(string args) int a = new int4; int max; for (int i = 0; i 4; i+) Console.WriteLine(请输入第0个数:,i+1); ai = Int32.Parse(Console.ReadLine(); max = a0; for (int j = 0; j max) max = aj; Console.WriteLine(其中最大值为:0, max); Console.Read(); 27、 分别用for,while,dowhile语句编写程序,实现求前n个自然数之和。class Program static void Main(string args) Console.Write(请输入1个整数:); int n = int.Parse(Console.ReadLine(); Console.WriteLine(使用For语句: + SumWithFor(n); Console.WriteLine(使用While语句: + SumWithWhile(n); Console.WriteLine(使用Do.While语句: + SumWithDoWhile(n); Console.ReadLine(); static int SumWithFor(int n) int sum = 0; for (int i = 1; i = n; i+) sum += i; return sum; static int SumWithWhile(int n) int sum = 0, i = 1; while (i = n) sum += i; i+; return sum; static int SumWithDoWhile(int n) int sum = 0, i = 1; do sum += i; i+; while (i = n); return sum; 28、 编程输出九九乘法表。public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) int i; int j; string a=; string, m = new string9, 9; for ( i=0;i 9;i +) for ( j = 0; j i + 1; j+) int sum = (i + 1) * (j + 1); mi, j = (i+1) + * + (j+1)+=+ sum; if (i = j) a = a + mi, j + rnrn; else a = a + mi, j + ; this.textBox1.Text = a; private void Form1_Load(object sender, EventArgs e) 29、 定义一个行数和列数相等的二维数组,并执行初始化,然后计算该数级两条对角线上的元素值之和。class Program static void Main(string args) int, nums = new int2, 2 2, 4 , 1, 3 ; string str = ; int n = 0; int m = 0; for (int i = 0; i 2; i+) str += nr; for (int j = 0; j 2; j+) str += numsi, j.ToString() + t; if (i = j) n = nums0, 0 + nums1, 1; else m = nums0, 1 + nums1, 0; str += nr; Console.WriteLine(数组:0正对角线的和为:1,负对角线的和为:2, str, n, m);30、建立一个一维数组,使用该数组列出所学习的课程名称。class Program static void Main(string args) ArrayList myAL = new ArrayList(); myAL.Add(XX); myAL.Add(YY); myAL.Add(ZZ); String strName = 课程名:; foreach (string st in myAL) strName += st + ; Console.WriteLine(strName); 31、编写程序,将一年中12个月,建立一个枚举类型数据,并对其进行调用。class Program enum month January, February, March, April, May, June, july, August, September, October, November, December ; static void Main(string args) Console.WriteLine(请输入1-12的月份:); int i = int.Parse(Console.ReadLine(); yuefen(i); public static void yuefen(int i) switch (i) case 1: Console.WriteLine(month.January); break; case 2: Console.WriteLine(month.February); break; case 3: Console.WriteLine(month.March); break; case 4: Console.WriteLine(month.April); break; case 5: Console.WriteLine(month.May); break; case 6: Console.WriteLine(month.June); break; case 7: Console.WriteLine(month.july); break; case 8: Console.WriteLine(month.August); break; case 9: Console.WriteLine(month.September); break; case 10: Console.WriteLine(month.October); break; case 11: Console.WriteLine(month.November); break; case 12: Console.WriteLine(month.December); break;32、 在窗体上建立一个标签,一个文本框,一个命令按钮,标签的text属性设置为“VC#程序设计”,设计一个程序,单击命令按钮,将标签上的信息显示在文本框中。public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) textBox1.Text = label1.Text; 33、设计一个简单的计算器,在文本框中,显示输入值和计算结果,用命令按钮做为数字键和功能键。public partial class Form1 : Form public Form1() InitializeComponent(); private void button13_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button1_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button2_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button3_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button5_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button6_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button7_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button9_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button10_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button11_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text += btn.Text; private void button15_Click(object sender, EventArgs e) textBox1.Text = ; private void button16_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text = textBox1.Text + + btn.Text + ; private void button14_Click(object sender, EventArgs e) double d_result; string s_txt = textBox1.Text; int space = s_txt.IndexOf( ); string s1 = s_txt.Substring(0, space); char operation = Convert.ToChar(s_txt.Substring(space+1),1); string s2 = s_txt.Substring(space + 3); MessageBox.Show(s_txt); double arg1 = Convert.ToDouble(s1); double arg2 = Convert.ToDouble(s2); switch (operation) case +: d_result = arg1 + arg2; break; case -: d_result = arg1 - arg2; break; case *: d_result = arg1 * arg2; break; case /: if (arg2 = 0) throw new ApplicationException(); else d_result = arg1 / arg2; break; default : throw new ApplicationException(); textBox1.Text = d_result.ToString(); private void button4_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text = textBox1.Text + + btn.Text + ; private void button8_Click(object sender, EventArgs e) Button btn = (Button)sender; textBox1.Text = textBox1.Text + + b
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年财务会计主管面试实战模拟题及答案解析
- 2025年特岗教师招聘考试英语学科高频考点解析
- 甲状腺癌护理常规
- 甲状腺亢进的护理课件
- 中班古诗课件教学内容
- 统编版语文七年级上册第8课《世说新语》二则练习题(含答案)
- 东北抗联精神教学课件
- 新解读《GB-T 36080-2018条码技术在农产品冷链物流过程中的应用规范》
- 生猪行业知识培训总结课件
- 生物酵素安全知识培训课件
- 2025年储能专业知识考试题库及答案
- 2025至2030年中国肽饲料市场供需现状及投资战略研究报告
- 化工机械法兰连接课件
- 面肌痉挛手术护理要点
- 情绪识别与营销-洞察及研究
- 室上性心动过速急救护理
- 2025年国家自然科学基金委员会招聘工作人员的(一)笔试模拟试题附答案详解
- 2025年村官、村干部相关法律知识考试题(附含答案)
- 工会考试试题及答案青岛
- 《中国成人呼吸系统疾病家庭氧疗指南(2024年)》解读 2
- 稻虾养殖技术课件
评论
0/150
提交评论