1、c#考试程序代码大全1)if-else private void button1_Click(object sender, EventArgs e) int s = int.Parse(textBox1.Text.Trim(); if (s = 90 & s = 80 & s = 70 & s = 60 & s = 0) double X1 = (-b + Math.Pow(delta, 0.5) / 2 * a; double X2 = (-b - Math.Pow(delta, 0.5) / 2 * a; string temp = X1=+X1.ToString()+rnX2=+X2.T
2、oString(); MessageBox.Show(temp); else MessageBox.Show(没有实数根!); 3)1-100之间所有整数的和,能被某数整除的所有整数的和,积private void button1_Click(object sender, EventArgs e) int sum = 0; for (int i = 0; i = 100; i = i + 1) sum = sum + i; textBox1.Text = sum.ToString(); 求1100之内所有偶数的和int sum = 0; for (int i = 0; i = 100; i =
3、 i + 2) sum = sum + i; textBox1.Text = sum.ToString();private void button1_Click(object sender, EventArgs e) int sum = 0; int a; for (int i = 0; i = 100; i+) a = i % 7; if (a = 0) sum = sum + i; textBox1.Text = sum.ToString(); 4)闰年的判断private void button1_Click(object sender, EventArgs e) int a = Con
4、vert.ToInt32(textBox1.Text); if(a %4=0 & a %100!=0) | a %400 = 0) MessageBox.Show (是闰年); else MessageBox.Show(不是闰年); 或办法int a = int.Parse(textBox1 .Text );string s = (a % 4 = 0 ? 该年份是闰年 : 该年份是平年 );MessageBox.Show(s )5)水仙花数private void button1_Click(object sender, EventArgs e) string s = 水仙花数:+rn; fo
5、r (int i = 100; i 1000; i+) int ge = i % 10; int shi = i / 10 % 10; int bai = i / 100; if (i = bai * bai * bai + shi * shi * shi + ge * ge * ge) s += i.ToString() + rn; MessageBox.Show(s,你好漂亮); 6)简单计算器private void button1_Click(object sender, EventArgs e) float num1 = Convert.ToInt32(textBox1.Text);
6、 float num2 = Convert.ToInt32(textBox3.Text); if (textBox2.Text =(+) MessageBox.Show(num1+num2).ToString(); else if(textBox2.Text =(-) MessageBox.Show(num1-num2).ToString(); else if(textBox2.Text =(*) MessageBox.Show(num1*num2).ToString(); else if(textBox2.Text =(/) MessageBox.Show(num1/num2).ToStri
7、ng(); else if(textBox2.Text =(%) MessageBox.Show(num1%num2).ToString(); else MessageBox.Show(运算符出错); 或办法double a = double.Parse(textBox1.Text);double b = double.Parse(textBox3.Text);string s = textBox2.Text;double c = (s = + ? a + b : (s = - ? a - b : (s = * ? a * b : (s = / ? a / b : a % b);Message
8、Box.Show(c.ToString();MessageBox.Show(c.ToString();7)一维数组的声明、初始化、赋值、遍历(求数组所有元素的和,积,最大值,最小值等统计数值)private void button1_Click(object sender, EventArgs e) textBox1.Text += 原数组为: + rn; int a = new int5; Random next = new Random(); for (int i = 0; i a.Length; i+) ai = next.Next(0, 100); textBox1.Text += a
9、i.ToString() + ; int max = a0, min = a0; int s = 0; int avg = 0; for (int i = 0; i max) max = ai; if (ai min) min = ai; s += ai; avg = s / a.Length; textBox1.Text += string.Format(rn最大值为0rn最小值为1rn均值为2rn和为3, max, min, avg, s); 8)二维数组的声明、初始化、赋值、遍历(求数组所有元素的和,积,最大值,最小值等统计数值)private void button2_Click(ob
10、ject sender, EventArgs e) textBox2.Text += 原数组为: + rn; Random next = new Random(); int, b = new int3, 4; for (int i = 0; i 3; i+) for (int j = 0; j 4; j+) bi, j = next.Next(-100,100); textBox2.Text += bi, j.ToString() + , ; int max = b0, 0, min = b0, 0; int s = 0; int avg = 0; for (int i = 0; i 3; i
11、+) for (int j = 0; j max) max = bi, j; if (bi, j C & A + C B & B + C A & A - B C & A - C B & B - C A) d = (A * A + C * C - B * B) / (2 * A * C); f = Math.Pow(1 - d * d), 0.5); s = 0.5 * A * C * f; MessageBox.Show(三角形面积是:+s.ToString(); else MessageBox.Show(输入数据有误); 11)乘法口诀表private void Form1_Load(obj
12、ect sender, EventArgs e) string line = ; int value = 0; lb199.Text = ; for (int i = 1; i 10; i+) line = ; for (int j = 1; j = 10) line += j.ToString() + * + i.ToString() + = + value.ToString() + ; else line += j.ToString() + * + i.ToString() + = + value.ToString() + ; lb199.Text += line + n; 12)字符串的
13、统计(大写,小写字母,数字的个数) public int sz, dx, xx, qt; private void button1_Click(object sender, EventArgs e) string a = textBox1.Text; foreach (char t in a) if (t = 0 & t = a & t = A & t = Z) dx+; else qt+; textBox2.Text = sz.ToString(); textBox3.Text = dx.ToString(); textBox4.Text = xx.ToString(); textBox5.
14、Text = qt.ToString();13)数值类型的转换:数值-字符串,字符串-数字,数值类型之间的显示转换,隐式转换类型转换、隐式转换、显示转换、Convert类的用法、ToString方法主要以隐式转换为例(其他的类似求解)补充:1)何时使用隐式和显示转换?答:隐式转换一般发生在数据进行混合运算的情况下,是编译系统自动进行的,不需要加以声明;显示转换,又称为强制类型转换,该方式需要用户明确的指定转换的目标类型,该类型一般形式为: (类型说明符) (需要转换的表达式)二、类型转换隐式转换short a; ushort b; int c; long d; float f; double
15、g; decimal h; char y; private void button1_Click(object sender, EventArgs e) b = 1; c = b; d = c; f = d; g = f; MessageBox.Show(g.ToString(), 转换1); private void button2_Click(object sender, EventArgs e) a = 2; c = a; d = c; f = d; g = f; MessageBox.Show(g.ToString(), 转换2);显示转换short a; ushort b; int
16、c; long d; float f; double g; decimal h; char y; private void button1_Click(object sender, EventArgs e) float a; int b; a = 1.1111f; b = (int)a; MessageBox.Show(b.ToString(), 郭浩); private void button2_Click(object sender, EventArgs e) decimal a; int b; a = 2.3m; b = (int)a; MessageBox.Show(b.ToStrin
17、g(); private void button3_Click(object sender, EventArgs e) long a; int b; a = 1; b = (int)a; MessageBox.Show(b.ToString(); private void button3_Click_1(object sender, EventArgs e) int a; char b; a = 94; b = Convert.ToChar(a); MessageBox.Show(b.ToString();Convert类的用法private void button1_Click(object
18、 sender, EventArgs e) string a = 123; int b = Convert.ToInt32(a); MessageBox.Show(b.ToString(); private void button2_Click(object sender, EventArgs e) string a = 456; double b = Convert.ToDouble(a); MessageBox.Show(b.ToString(); private void button3_Click(object sender, EventArgs e) string a = 789;
19、decimal b = Convert.ToDecimal(a); MessageBox.Show(b.ToString(); private void button4_Click(object sender, EventArgs e) string a = 0; char b = Convert.ToChar(a); MessageBox.Show(b.ToString(); ToString方法double a =double.Parse (textBox1.Text);MessageBox.Show(a.ToString() ); 14)遍历窗体上的组件Foreach 遍历组件代码如下i
20、nt count_textbox = 0; int count_checkbox = 0; int count_button = 0; int count_listbox = 0; foreach (Control c in this.Controls) if (c is TextBox) count_textbox = count_textbox + 1; if (c is CheckBox) count_checkbox = count_checkbox + 1; if (c is Button) count_button = count_button + 1; if (c is List
21、Box) count_listbox = count_listbox + 1; string result = CheckBox的数量: + count_checkbox + rn + TextBox的数量: + count_textbox + rn + Button的数量: + count_button + rn + ListBox的数量: + count_listbox + rn; MessageBox.Show(result, 遍历组件, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.B
22、utton1);运行截图15)随机数发生器的用法求浮点型数组最大元素textBox1.Text = 生成数组: + rn; int a = new int5; Random r = new Random(); for (int i = 0; i a.Length; i+) ai = r.Next(0, 1000); textBox1.Text += ai.ToString() + ; int max = a0, min = a0; for (int i = 0; i max) max = ai; if (ai = 0 & s = 100) switch (s / 10) case 0: case 1: case 2: case 3: case 4: case 5: MessageBox.Show(不及格); break; case 6: MessageBox.Show( 及格); break; case 10: MessageBox.Show( 优秀); break; default: MessageBox.Show(请输入正确的分数); break; else MessageBox.Show( 请输入正确的分数); private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) str
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1