1、从键盘输入一个正整数,按数字的相反顺序输出。
2、从键盘上输入两个整数,由用户回答它们的和,差,积,商和取余运算结果,并统计出正确答案的个数。
3、写一条for语句,计数条件为n从100~200,步长为2;然后再用while语句实现同样的循环。
4、编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。判断依据如下:
等级={优 (90~100分);良 (80~89分);中 (60~69分);差 (0~59分);}
Console.WriteLine("你考了多少分?(1~100)");
int a = int.Parse(Console.ReadLine());
switch (a / 10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("等级为差");
break;
case 6:
Console.WriteLine("等级为中");
break;
case 7:
case 8:
Console.WriteLine("等级为良");
break;
case 9:
case 10:
Console.WriteLine("等级为优");
break;
default:
Console.WriteLine("输入有误,请输入0-100的数。");
break;
}
Console.ReadLine();
}
5、输入一个整数,将各位数字反转输出。
static void Main(string[] args)
{
Console.WriteLine("请输入数字:");
string str;
str = Console.ReadLine();
Console.WriteLine("反序后为;");
change(ref str);
}
static void change(ref string str1)
{
int m;
int a = 0;
m = str1.Length;
char[] arr = new char[m];
for (int i = str1.Length - 1; i >= 0; i--)
{
arr[a] = str1[i];
a++;
}
foreach (char b in arr)
{
Console.Write("{0}", b);
}
Console.ReadLine();
}
6、使用穷举法并分别用for、while、do…while循环语句求出1~100之间的质数。
static void Main(string[] args)
{
Console.WriteLine("使用For语句:");
WithFor();
Console.WriteLine();
Console.WriteLine("使用While语句:");
WithWhile();
Console.WriteLine();
Console.WriteLine("使用Do...While语句:");
WithDoWhile();
Console.ReadLine();
}
static void WithFor()
{
int i, j;
for (i = 1; i < 100; i++)
{
for (j = 2; j <= i / 2; j++)
{
if (i % j == 0)
break;
}
if (j > i / 2)
Console.Write(i+" ");
}
}
static void WithWhile()
{
int i=1, j, n;
while (i < 100)
{
n = 1;
j = 2;
while (n == 1 && j < i)
{
if (i % j == 0) n = 0;
j++;
}
if (n == 1)
Console.Write(i + " ");
i++;
}
}
static void WithDoWhile()
{
{
int i, j, n;
i = 1;
do
{
n = 1; j = 2;
do
{
if (i % j == 0 && i != 2)
{
n = 0;
break;
}
j++;
} while (j <= i / 2);
if (n==1)
Console.Write(i + " ");
i++;
} while (i <= 100);
}
}
7、求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
class Program
{
static void Main(string[] args)
{
int a = 0;
int[] num = new int[1000];
Console.WriteLine("1000以内能被7整除的数有:");
for (int i = 1; i <= 1000; i++)
{
if (i % 7 == 0)
{
Console.Write("{0} ", i);
a++;
}
}
Console.WriteLine();
Console.WriteLine("以上每五个数之和是");
int n; int c = 0;
for (int i = 1; i < 1001; i++)
{
if (i % 7 == 0)
{
num[c] = i;
c++;
}
}
for (int b = 0; b + 4 < a; b = b + 5)
{
n = num[b] + num[b + 1] + num[b + 2] + num[b + 3] + num[b + 4];
Console.Write(n + " ");
} Console.ReadLine();
}
}
8、编写一个控制台程序,分别输出1~100之间的平方、平方根、自然对数、e指数的数学用表。
static void Main(string[] args)
{
{
for (int i = 1; i < 101; i++)
{
Console.Write("{0}的平方是:{1}\t" ,i,i * i );
Console.Write("{0}的平方根是:{1}\t" ,i, Math.Sqrt(i));
Console.Write("以e为底{0}的对数值是:\t", i, Math.Log(i));
Console.Write("e的{0}次方值是\t", i, Math.Exp(i));
Console.WriteLine();
}
Console.ReadLine();
}
}
9、设计一个包含多个构造函数的类,并分别用这些构造函数实例化对象。
static void Main(string[] args)
{
Console.WriteLine("圆周长:");
perimeter juxingperimeter = new perimeter(2.5);//圆周长
juxingperimeter.showResult();
Console.WriteLine("矩形周长:");
perimeter yuanperimeter = new perimeter(2, 3);//矩形周长
yuanperimeter.showResult();
Console.WriteLine("三角形周长:");
perimeter shanjiaoperimeter = new perimeter(1, 2, 3);//三角形周长
shanjiaoperimeter.showResult();
Console.ReadLine();
}
}
class perimeter//周长类
{
public double myperimeter = 0;
public void showResult()
{
Console.WriteLine("{0}", myperimeter);
}
public perimeter(double r)
{
myperimeter = 3.1415 * 2 * r;
}
public perimeter(double x, double y)
{
myperimeter = x + y;
}
public perimeter(double a, double b, double c)
{
myperimeter = (a + b + c);
}
10、编写一个矩形类,私有数据成员为举行的长(len)和宽(wid),无参构造函数将len和wid设置为0,有参构造函数设置和的值,另外,类还包括矩形的周长、求面积、取举行的长度、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。
11、编写一个类,要求带有一个索引器可以存储100个整型变量。
12、编写一个类Cal1,实现加、减两种运算,然后,编写另一个派生类Cal2,实现乘、除两种运算。
13、建立三个类:具名、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。要求每个类中都提供数据输入输出的功能。
14、编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入字符串");
string str = Console.ReadLine();
orderby.sort(str);
}
}
class orderby
{
public static void sort(string str)
{
if ((int)str[0] >= 48 && (int)str[0] <= 57) //字符串变量可以看做是char变量,用int取ascii码,判断是不是整数
{
char[] num = str.ToCharArray();//调用字符串的tochararray方法,将str转化字符数组
for (int i = 1; i < num.Length; i++)//冒泡排序
{
for (int j = 0; j < num.Length - i; j++)
{
if (num[j] > num[j + 1])//比较ascii码
{
char temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
foreach (char a in str)
{ Console.Write(a); }
}
}
else//字符串反序输出
{
for (int i = str.Length-1; i >=0; i--)
{
Console.Write(str[i]);
}
}
}
15、设计一个类,要求用事件每10秒报告机器的当前时间。
16、编写一个窗体程序,用菜单命令实现简单的加、减、乘、除四则运算,并将结果输出到对话框。
17、编写一个具有主菜单和快捷菜单的程序,实现文本文件的打开、修改和保存。
18、在label控件中随机输入20个1~1000之间的整数,求出其中所有的素数的和。
19、编写一个程序,通过使用主菜单和工具栏按钮实现与Window记事本间的文本数据拷贝。
20、仿照word中的“文件打开”对话框界面,编制一个自己的文件打开模式对话框。
21、自己编写一个控件,使得该控件放置在窗体上之后,可以通过拖动四个顶点随意地改变控件的形状。(提示:在控件的Paint事件过程中编写外观绘制代码)
22、编写一个程序,将一幅位图显示在一个图片框中,对位图惊醒45度旋转后,将图中所有的红色替换为黑色,然后存盘。
23、编写一个控制台程序,分别将字符串“hello,my friend”写入文件f1.txt,然后,将数据分别以整型、布尔型、双精度型、字符型读出并显示。
30. 编写一个程序,从键盘上输入3个数,输出这3个数的积及它们的和。要求编写成控制台应用程序。
31.编写一个程序,输入梯形的上底,下底和高,输出梯形的面积。要求编写成Window应用程序。
32. 编写一个进行加减乘除四则运算的程序,要求:输入两个单精度数,然后输入一个运算符号,输出两个单精度数进行运算后的结果。要求编写为控制台程序。
33. 兔子繁殖问题。设有一对新生的兔子,从第三个月开始他们每个月都生一对兔子,新生的兔子从第三个月开始又每个月生一对兔子。按此规律,并假定兔子没有死亡,20个月后共有多少个兔子?要求编写为控制台程序。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetRabbitCount(20));
}
static int GetRabbitCount(int monthCount)
{
int m = monthCount / 3;
return (int)Math.Pow(2, m) * 2;
}
}
34. 编写程序,把由10个元素组成的一维数组逆序存放再输出。
static void Main(string[] args)
{
int[] myArray1 = new int[10] { 9, 6, 10, 2, 5, 7, 56, 89, 0, 4 };
int[] myArray2 = new int[10];
for (int i = myArray1.Length - 1; i >= 0; i--)
{
myArray2[9 - i] = myArray1[i]; //一维数组逆序存放
}
Console.WriteLine("原数组为:");
foreach (int a in myArray1)
{
Console.WriteLine(a);
}
Console.WriteLine("逆序存放后的数组为:");
foreach (int b in myArray2)
{
Console.WriteLine(b);
}
Console.ReadLine();
}
35. 编写程序,统计4X5二维数组中奇数的个数和偶数的个数。
static void Main(string[] args)
{
int[,] list = new int[4, 5] { {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},
{16,17,18,19,20}};
//for循环输出原数组
for (int i = 0; i < 4; i++)
{
for (int b = 0; b < 5; b++)
{
Console.Write(list[i, b].ToString() + ",");
}
Console.WriteLine();
}
int oddnum = 0;
int evennum = 0;
//for循环计算奇偶个数
for (int i = 0; i < 4; i++)
{
for (int b = 0; b < 5; b++)
{
if (list[i, b] % 2 == 0)
{ evennum++; }
else
{ oddnum++; }
}
}
Console.WriteLine("这个数组中偶数个数是{0}," + "奇数个数是{1},", evennum, oddnum);
Console.ReadLine();
}
36. 编写一个求整数任意位数字的过程,过程的调用形式为:digit(n,k),其功能是取出数n从右边起的第K位数字,例如:digit(1234,3)=2, digit(1234,4)=1, digit(1234,6)=0。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个整数n:");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入第k位数字:");
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("所求的整数{0}从右边起第{1}位数字位:{2}", n, k, digit(n, k));
Console.ReadLine();
}
static int digit(int num, int k)
{
int s = 0;
for (int i = 1; i < k; i++)
{
num = num / 10;
}
s = num % 10;
return s;
}
}
37. 编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:
‘a’→’d’ ‘b’→’e’ ‘w’→’z’ …… ‘x’→’a’ ‘y’→’b’ ‘z’→’c’
‘A’→’B’ ‘B’→’E’ ‘W’→’Z’ …… ‘X’→’A’ ‘Y’→’B’ ‘Z’→’C’
对于其他字符,不进行加密。
class Program
{
static void Main(string[] args)
{
char zm;
while (true)
{
Console.WriteLine("请输入密码(a到z)");
string str = Console.ReadLine();
Console.WriteLine("加密后:");
int length = str.Length;
if (str == "*****")
break;
for (int i = 0; i < length; i++)
{
zm = str[i];
if ((zm >= 'a' && zm <= 'w') || (zm >= 'A' && zm <= 'W'))
zm = Convert.ToChar(zm + 3);
if ((zm > 'W' && zm <= 'Z') || (zm > 'w' && zm <= 'z'))
zm = Convert.ToChar(zm - 23);
Console.Write(zm);
}
Console.ReadLine();
}
}
}
38. 定义一个车辆(Vehicle)基类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed(最大速度)、Weight(重量)等域。然后以该类为基类,派生出bicycle、car等类。并编程对该派生类的功能进行验证。
class Program
{
static void Main(string[] args)
{
bicycle bic1 = new bicycle();
bic1.bic_run();
car car1 = new car();
car1.car_stop();
Console.ReadLine();
}
}
class Vehicle
{
public void Run()
{
Console.WriteLine("i can run");
}
public void Stop()
{
Console.WriteLine("i can stop");
}
protected float Speed = 1, MaxSpeed = 2, Weight = 3;
}
class bicycle : Vehicle
{
public void bic_run()
{
Vehicle v1 = new Vehicle();
v1.Run();
Console.WriteLine(Speed);
}
}
class car: Vehicle
{
public void car_stop()
{
Vehicle v1 = new Vehicle();
v1.Stop();
Console.WriteLine(MaxSpeed);
}
}
39. 编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后对Person 类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出三个形式。最后编程对student类的功能进行验证。
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三", 22, "男", 78.6, 77, 93.2, 95.3, 88.9);
Console.WriteLine("平均成绩:" + s.Average());
}
}
//person类
public class Person
{
//域表示与类或对象相关联的变量,注释部分可剩
string name, sex;//域
int age;//域
/*public string Name
{
get { return name; }//向外界开放域i的访问
set { name = value; }//对域name赋值
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}*/
public Person() { }
public Person(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
}
//student类
public class Student : Person
{
double english,chinese,mathematics,music,history;
/*public double English
{
get { return english; }
set { english = value; }
}
public double Chinese
{
get { return chinese; }
set { chinese = value; }
}
public double Mathematics
{
get { return mathematics; }
set { mathematics = value; }
}
public double Music
{
get { return music; }
set { music = value; }
}
public double History
{
get { return history; }
set { history = value; }
}*/
public Student() { }
public Student(string name, int age, string sex)
: base(name, age, sex)
{
this.english = 0;
this.chinese = 0;
this.mathematics = 0;
this.music = 0;
this.history = 0;
}
public Student(string name, int age, string sex, double english, double chinese, double mathematics, double music, double history)
: base(name, age, sex)
{
this.english = english;
this.chinese = chinese;
this.mathematics = mathematics;
this.music = music;
this.history = history;
}
public double Average()
{
return (english + chinese + mathematics + music + history) / 5;
}
}
40. 编写一个冒泡法排序程序,要求在程序中能够捕获到数组下标越界的异常。
class Program
{
static void Main(string[] args)
{
int[] arr = new int[]{2,3,1};
for(int i=0;i<arr.Length;i++)
{
for(int j=arr.Length-1;j>=0;j--)
{
int tmp = arr[0];
arr[0] = arr[1];
arr[1] = tmp;
}
}
try
{
for(int i=0;i<=arr.Length;i++)
{
Console.WriteLine(arr[i]);
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
41.编写一个计算器程序,要求在程序中能够捕获到被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();
}
}
}
45 编程输出1~100中能被3整除但不能被5整除的数,并统计有多少个这样的数。
class Program
{
static void Main(string[] args)
{
{
Console.WriteLine("1~100中能被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();
}
}
}
46. 编程输出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; j++)
{
if (i % j == 0)
break;
}
if (j > i / 2)
Console.Write(i.ToString()+' ');
}
Console.ReadLine();
}
}
47. 编写一个程序,对输入的4个整数,求出其中最大值和最小值。
class Program
{
static void Main(string[] args)
{
int[] a = new int[4];
int max;
for (int i = 0; i < 4; i++)
{
Console.WriteLine("请输入第{0}个数:",i+1);
a[i] = Int32.Parse(Console.ReadLine());
}
max = a[0];
for (int j = 0; j < 4; j++)
{
if (a[j] > max)
max = a[j];
}
Console.WriteLine("其中最大值为:{0}", max);
Console.Read();
}
}
48. 分别用for,while,do…while语句编写程序,实现求前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;
}
}
49. 编程输出九九乘法表。
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 string[9, 9];
for ( i=0;i <9;i ++)
{ for ( j = 0; j < i + 1; j++)
{
int sum = (i + 1) * (j + 1);
m[i, j] = (i+1) + "*" + (j+1)+"="+ sum;
if (i == j)
a = a + m[i, j] + "\r\n\r\n";
else
a = a + m[i, j] + " ";
this.textBox1.Text = a;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
50. 定义一个行数和列数相等的二维数组,并执行初始化,然后计算该数级两条对角线上的元素值之和。
class Program
{
static void Main(string[] args)
{
int[,] nums = new int[2, 2] { { 2, 4 }, { 1, 3 } };
string str = "";
int n = 0;
int m = 0;
for (int i = 0; i < 2; i++)
{
str += "\n\r";
for (int j = 0; j < 2; j++)
{
str += nums[i, j].ToString() + "\t";
if (i == j)
{
n = nums[0, 0] + nums[1, 1];
}
else
{
m = nums[0, 1] + nums[1, 0];
}
}
} str += "\n\r";
Console.WriteLine("数组:{0}正对角线的和为:{1},负对角线的和为:{2}", str, n, m);
}
}
51. 建立一个一维数组,使用该数组列出所学习的课程名称。
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);
}
}
52. 编写一个包含学生基本资料的结构类型数据(要求包括姓名,性别,年龄,身高,体重等)。class Program
{
static void Main(string[] args)
{
}
struct StudentInfo
{
string name;
int age;
float height;
float weight;
}
}
53. 编写程序,将一年中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;
}
}
}
54. 在窗体上建立一个标签,一个文本框,一个命令按钮,标签的text属性设置为“VC#程序设计”,设计一个程序,单击命令按钮,将标签上的信息显示在文本框中。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = label1.Text;
}
}
55. 设计一个简单的计算器,在文本框中,显示输入值和计算结果,用命令按钮做为数字键和功能键。
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 + " " + btn.Text + " ";
}
private void button12_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
}
56. 在窗体上建立一个列表框,一个文本框和一个命令按钮,在列表框中列有本班10个同学的姓名,当选中某个学生姓名后,单击此命令按钮,则在文本框中显示该学生的籍贯。
public partial class Form1 : Form
{
private Hashtable students = new Hashtable();
private String[] names;
public Form1()
{
InitializeComponent();
students.Add("王大", "河南省");
students.Add("王二","福建省");
students.Add("王三", "广东省");
students.Add("王四", "河北省");
students.Add("王五", "青岛省");
students.Add("王六", "葫芦浩特");
names = new string[students.Keys.Count];
students.Keys.CopyTo(names,0);
listBox1.Items.AddRange(names);
}
private void button1_Click(object sender, EventArgs e)
{
string name = listBox1.SelectedItem.ToString();
textBox1.Text = students[name].ToString();
}
}
57. 用定时器控件按秒计时,在窗体上创建一个标签,程序执行后在标签内显示经过的秒。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
i++;
label1.Text = i.ToString()+'秒';
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
61. 编写一个程序,检查一个字符变量的值是否为T或t。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符:");
char b = Convert.ToChar(Console.ReadLine());
if (b == 't' )
{ Console.WriteLine("字符变量为t"); }
else if (b == 'T')
{ Console.WriteLine("字符变量为T"); }
else
{ Console.WriteLine("不满足要求"); }
}
}
68. 创建一个类,它存储一个int数据成员MyNumber,并给该数据成员创建属性,当该数据成员被存储时,将其乘以100;当其被读取时,将其除以100。
class Program
{
int MyNumber;
public int shuxing
{
get
{
return MyNumber / 100;
}
set
{
MyNumber = value * 100;
}
}
static void Main(string[] args)
{
Program a = new Program();
a.shuxing = 100;
Console.WriteLine(a.shuxing);
}
}
71. 为方法myMethod编写方法头。该方法接受三个参数。第一个名为myVal,其数据类型为double,并按值传递;第二个是一个输出变量,名为myOutput;第三。个是按引用传递的,数据类型为int,名为myRerence。另外该方法是公有的,其返回类型为byte。
class Program
{
static void Main(string[] args)
{
}
// 方法头就是:
//修饰符+返回类型 +方法名(形参列表){
// 方法体}
public static byte fangfatou(double maVal, out double myOutput, ref int myRerence)
{
}
}
73. 为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。
class Program
{
static void Main(string[] args)
{
//注意随机值的使用方法
Random rand = new Random();
int[] student = new int[30];
int sum = 0; double a;
for (int i = 0; i < 30; i++)
{
student[i] = rand.Next(1, 101);
sum += student[i];
}
a = sum / 30.0;
Console.WriteLine(a);
Console.Read();
}
}
74.为名为abc的公有函数编写方法头,该函数接受两个short参数,返回值类型为byte。
创建一个使用这两个类的应用程序类。
class Program
{
static void Main(string[] args)
{
short m = 2;
short n = 3;
byte d = abc(m,n);
Console.WriteLine(d);
}
public static byte abc(short a, short b)
{
short c;
c = (short)(a * b);
return (byte)c;
}
}
77.编写为ABC类声明构造函数的方法头,它接受两个int 参数ARG1和ARG2。该构造函数调用基类的构造函数,并将ARG2传递给它。调用是在方法头中完成的:
Public ABC ( int ARG1,int ARG2 ):base (ARG2)
{
}
public class A
{
public A(int avg)
{
}
}
class ABC:A
{
public ABC(int avg1, int avg2)
: base(avg2)
{
}
}
78. 以“星期几,月份,日和四位年份”格式(如Monday,January 1,2002) 打印日期值的代码。
79. 一个这样的程序:让用户输入其全名,年龄和电话号码,以特定的格式显示这些消息,并显示用户的姓名的首字母。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
int age = int.Parse(textBox2.Text);
string phone = textBox3.Text;
textBox4.Text = "姓名:" + name + "\r\n" + "年龄:" + age + "\r\n" + "电话:" + phone + "\r\n"+"用户姓名的首字母是:"+name[0];
}
}
80. 为Iid的接口编写代码,该接口只包含一个名为ID的属性成员。
public interface Lid
{
int ID;
}
class Program
{
static void Main(string[] args)
{
}
}
81. 声明一个名为Iposition的接口的代码。该接口包含一个接受一个Point值,并返回一个布尔值的方法。
public interface Iposition
{
bool back(Point point);
}
class Program
{
static void Main(string[] args)
{
}
}
82.编写一个使用代表的程序,对整型数组中的元素进行排序。
class Program
{
private delegate bool isFirst(int num1,int num2);
static void Main(string[] args)
{
int[] array = new int[] {1,4,3,6,7,5 };
Program.isFirst maxSort = new Program.isFirst(Sorter.max);
Program.isFirst minSort = new Program.isFirst(Sorter.min);
sort(array,minSort);
foreach(int num in array){
Console.Write(num);
Console.Write(" ");
}
}
static void sort(int[] array,isFirst isFirstDelegate)
{
for (int i = 1; i < array.Length;i++)
{
for (int j = 0; j < array.Length - i;j++)
{
if(isFirstDelegate(array[j],array[j+1])){
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
}
83.创建一个程序,它使用二进制文件方法来写文件。创建一个用于存储人的姓名、年龄、会员资格的结构。将这些信息写入文件中(提示:年龄可以是整数,会员资格可以是布尔型)。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
i = int.Parse(this.textBox1.Text);
if (i <= 100)
label1.Text = "您输入的值在1-100之间";
else
label1.Text = "您输入的值不在1-100之间";
}
}
86.创建一个窗体,该窗体包括一个可用来输入数字的文本框,当用户单击按钮后,在标签中显示一条消息,指出该数字是否位于0-100之间。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
i = int.Parse(this.textBox1.Text);
if (i <= 100)
label1.Text = "您输入的值在1-100之间";
else
label1.Text = "您输入的值不在1-100之间";
}
}
87.编写这样的代码:将名为butnl 和butn2 的单选按钮控件加入到一个名为grbox的组合框中。
88.创建一个使用ColorDialog 对话框的应用程序。将应用程序主窗体的背景颜色设置为ColorDialog 返回的颜色。返回的颜色被存储在Color属性中。提示:创建一个ColorDialog变量,调用该对话框时,选择的颜色应该存储在Color属性中。
89.创建一个包含菜单的应用程序。用户选择菜单时,将显示一个对话框,对话框中包含大量的控件,其中一个是ok按钮。
90. 一个控制台应用程序,输出1~5的平方值,要求:
用for语句实现。
用while语句实现。
用do-while语句实现。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输出1-5的平方值");
Console.WriteLine("使用For语句:");
SumWithFor();
Console.WriteLine("使用While语句:");
SumWithWhile();
Console.WriteLine("使用Do...While语句:");
SumWithDoWhile();
Console.ReadLine();
}
static void SumWithFor()
{
for (int i = 1; i <= 5; i++)
{ Console.WriteLine("{0}的平方是{1}",i,i*i); }
}
static void SumWithWhile()
{
int i = 1;
while (i <= 5)
{
Console.WriteLine("{0}的平方是{1}", i, i * i);
i++;
}
}
static void SumWithDoWhile()
{
int i = 1;
do
{
Console.WriteLine("{0}的平方是{1}", i, i * i);
i++;
}
while (i <= 5);
}
}
91. 一个控制台应用程序,要求用户输入5个大写字母,如果用户输入的信息不满足要求,提示帮助信息并要求重新输入。
static void Main(string[] args)
{
start:
Console.WriteLine("请输入五个大写字母");
string str = Console.ReadLine();
char[] arr = str.ToCharArray();
if (arr.Length > 5)
{
Console.WriteLine("输入错误");
goto start;
}
for(int i=0;i<arr.Length;i++)
{
if (arr[i] >= 'A' && arr[i] <= 'Z')
{
if(i==4)
Console.WriteLine("输入正确");
}
else
{
Console.WriteLine("输入错误");
goto start;
}
}
}
}
92. 一个控制台应用程序,要求完成写列功能。
1)接收一个整数n。
2)如果接收的值n为正数,输出1~n间的全部整数。
3)如果接收的值n为负值,用break或者return退出程序。
4)转到A继续接收下一个整数。
static void Main(string[] args)
{
start:
int i = 0;
Console.WriteLine("请输入一个整数N:");
i = int.Parse(Console.ReadLine());
if (i >= 0)
{
Console.Write("1到{0}之间全部的整数为", i);
int j = 1;
while (j <= i)
{
Console.Write(j.ToString() + ' ');
j++;
}
Console.WriteLine();
}
else
{
return;
}
goto start;
}
}
93. 一个控制台应用程序,求1000之内的所有“完数”。所谓“完数”是指一个数恰好等于它的所有因子之和。例如6是完数,因为6=1+2+3。
class Program
{
static void Main(string[] args)
{
string output = "";
int i, j, s;
for (i = 2; i <= 1000; i++)
{
s = 0;
for (j = 1; j < i; j++)
if (i % j == 0)
s = s + j;
if (i == s)
output += " " + i;
}
Console.WriteLine(output.ToString());
}
}
94.编写一个控制台应用程序,完成下列功能:
1)创建一个类,用无参数的构造函数输出该类的类名。
2)添加一个重载的构造函数,带有一个String类型的参数,在此构造函数中将传递的字符串打印出来。
3)在Main方法中创建属于这个类的一个对象,不传递参数。
4)在Main方法中创建属于这个类的另一个对象,传递一个字符串“This is a string.”。
5)在Main方法中声明类型为这个类的一个具有5个对象的数组,但不要实际创建分配到数组里的对象。
6)写出运行程应该输出的结果。
95.编写一个控制台应用程序,定义一个类 MyClass,类中包含有public、private以及protected数据成员及方法。然后定义一个从MyClass类继承的类MyMain,将Main方法放在MyMain中。在Main方法中创建MyClass类的一个对象,并分别访问类中的数据成员及方法。要求注明在试图访问所有类成员时哪些语句会产生编译错误。
class MyMain:MyClass
{
static void Main(string[] args)
{
MyClass mytime = new MyClass();
mytime.set_time();
// Console.WriteLine(mytime.time[1]);//编译错误,私有成员
// mytime.show_time();//编译错误,好像在static方法中不能被访问
}
void show()
{
MyClass mytime = new MyClass();
mytime.set_time();
show_time();//编译正确
}
}
class MyClass
{
string[] time = new string[3];
public void set_time()
{
Console.WriteLine("请输入当前时间(例如10:10:09)");
time = Console.ReadLine().Split(':');
}
protected void show_time()
{
Console.WriteLine("现在的时间是:");
string str="";
foreach (string a in time)
{
str+=a.ToString();
}
Console.WriteLine(str + ' ');
}
}
97.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能:
1)输出字符串的长度。
2)输出字符串中第一个出现字母a的位置。
3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。
4)将字符串“hello”替换为“me”,输出新字符串。
5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。
class Program
{
public static string str1;
public static void find(string str)
{
int n = 0;
n = str.IndexOf("a");
Console.WriteLine("第一次出现a的位置为{0}", n+1);
}
public static void replace(string str)
{
str1=str.Replace("hello","me");
Console.WriteLine("将hello替换后的字符串是{0}", str1);
}
public static void insert(string str)
{
str1=str.Insert(3, "hello");
Console.WriteLine("插入hello后的字符串为{0}", str1);
replace(str1);
}
public static void divide(string str)
{
insert(str);
string[] str2=new string[100];
str2 = str1.Split('m');
Console.WriteLine("分离后的结果是");
foreach(string a in str2)
{
Console.WriteLine(a);
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入长度大于3的字符串");
string str = Console.ReadLine().Trim();
if (str.Length < 3)
{
Console.WriteLine("请重新输入长度大于3的字符串");
str = Console.ReadLine().Trim();
}
else
{
Console.WriteLine("您输入的字符串长度为{0}", str.Length);
}
find(str);
divide(str);
}
}
98.编写一个控制台应用程序,完成下列功能,并回答提出的问题。
1)创建一个类A,在构造函数中输出“A”,在创建一个类B,在构造函数中输出“B”。
2) 从A继承一个名为C的新类,并在C内创建一个成员B。不要为C创建构造函数。
3)在Main方法中创建类C的一个对象,写出运行程序后输出的结果。
4)如果在C中也创建一个构造函数输出“C”,整个程序运行的结果又是什么?
public class A
{
public A()
{
Console.WriteLine("A");
}
}
public class B
{
public B()
{
Console.WriteLine("B");
}
}
public class C : A
{
B newb = new B();
}
class Program
{
static void Main(string[] args)
{
C newc = new C();
Console.ReadLine();
}
}
99.编写一个控制台应用程序,完成下列功能,并写出运行程序后输出的结果。
1)创建一个类A,在A中编写一个可以被重写的带int类型参数的方法MyMethod,并在该方法中输出传递的整型值后加10后的结果。
2)再创建一个类B,使其继承自类A,然后重写A中的MyMethod方法,将A中接收的整型值加50,并输出结果。
3)在Main方法中分别创建A和类B的对象,并分别调用MyMethod方法。
public class A
{
public virtual void MyMethod(int num)
{
num += 10;
Console.WriteLine(num);
}
}
public class B : A
{
public override void MyMethod(int num)
{
num += 50;
Console.WriteLine(num);
}
}
class Program
{
static void Main(string[] args)
{
A newa = new A();
newa.MyMethod(2);
B newb = new B();
newb.MyMethod(2);
Console.ReadLine();
}
}
100.写出符合下列要求的数据库连接字符串以及所使用的数据库连接类。
1)数据库服务器地址为202.196.131.26,登录用户名为sa,密码为administrator,后台数据库为Microsoft SQL Server数据库,要连接的数据库名为test.
2) 据库为Access数据库,数据库文件存放在运行程序所在目录的\db子目录下,文件名为jobtypeDB.Mdb。
3)数据库为FoxPro数据库,数据库目录为:\studentDB,数据表为grade.dbf。
class Program
{
public static SqlConnection ConSQL()
{
string conString = "server=202.196.131.26;database=test;uid=sa;pwd=administrator";
SqlConnection con = new SqlConnection(Constring);
return con;
}
public static OleDbConnection ConAccess()
{
string currentDir = System.IO.Directory.GetCurrentDirectory;
string path = currentDir + "\\db\\jobtypeDB.Mdb";
string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";User Id=admin;Password=;";
OleDbConnection conn = new OleDbConnection(conString);
}
}