1.用户输入一个整数,用if...else判断是偶数还是奇数
Console.WriteLine("请输入一个整数:");
int a = Convert.ToInt32(Console.ReadLine());
if (a / 2 == 0)
{
Console.WriteLine("偶数");
}
else
{
Console.WriteLine("奇数");
}
2.输入一个字母,判断是大写还是小写字母
Console.WriteLine("请输入一个字母:");
char ch = char.Parse(Console.ReadLine());
if (ch > 'a' && ch < 'z')
{
Console.WriteLine("小写");
}
else
Console.WriteLine("大写");
3.求1~99所有奇数的和,用while语句
int i = 0, sum = 0;
while (i<100)
{
sum += i;
i++;
}
Console.WriteLine(sum);
4.用户输入三个整数,将最大数和最小数输出
Console.WriteLine("请输入第1个数:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第2个数:");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第3个数:");
int c = Convert.ToInt32(Console.ReadLine());
int max = Math.Max(Math.Max(a, b), c);
int min = Math.Min(Math.Min(a, b), c);
Console.WriteLine("max={0},min={1}",max,min);
5.输入三个数,按从小到大的顺序排列
比较特殊的做法:
int[] num = new int[3];
Console.WriteLine("请输入第1个数");
num[0] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第2个数");
num[1] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第3个数");
num[2]= Convert.ToInt32(Console.ReadLine());
//int min = num[0] < num[1] ? (num[0] < num[2] ? num[0] : num[2]) : (num[1] < num[2] ? num[1] : num[2]);
int min =Math.Min(Math.Min(num[0],num[1]),num[2]);
int max = Math.Max(Math.Max(num[0],num[1]),num[2]);
for (int i = 0; i < 3; i++)
{
if (num[i]<max&&num[i]>min)
{
Console.WriteLine("{0},{1},{2}",min,num[i],max);
}
}
一般的做法:
Console.WriteLine("请输入第1个数:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第2个数:");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第3个数:");
int c = Convert.ToInt32(Console.ReadLine());
int temp;
if (a > b) { temp = a; a = b; b = temp; }
if (a > c) { temp = a; a = c; c = temp; }
if (b > c) { temp = b; b = c; c = temp; }
Console.WriteLine("{0},{1},{2}",a,b,c);
6.将1~200末位数为5的整数求和
int sum = 0;
for (int i = 1; i <= 200; i++)
{
if (i % 5==0)
{
sum += i;
}
}
Console.WriteLine(sum);
7.计算2.5的3次方
方法一:
double sum =1;
for (int i = 0; i < 3; i++)
{
sum *= 2.5;
}
Console.WriteLine(sum);
方法二:
Console.WriteLine(Math.Pow(2.5, 3));
8.将24的所有因子求积
int sum = 0;
for (int i = 1; i <= 24; i++)
{
if (24%i==0)
{
sum += i;
}
}
Console.WriteLine(sum);
9.输入一个年份看是否为闰年
int i = Convert.ToInt32(Console.ReadLine());
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
Console.WriteLine("闰年");
}
else
Console.WriteLine("平年");
10.输入一段字符判断是大写,还是小写。若是小写,转换为大写,若是大写,转换为小写
string a = Convert.ToString(Console.ReadLine());
char b = char.Parse(a);
if (b >= 'a' && b <= 'z')
{
Console.WriteLine(a.ToUpper());
}
else
Console.WriteLine(a.ToLower());
11.判断一个数是否为素数(质数)
int b = 1;
int a = Convert.ToInt32(Console.ReadLine());
for (int i = 2; i <= a/2; i++)
{
if (a % i == 0)
{
b = 0;
break;
}
}
if (b == 0)
{
Console.WriteLine("不是素数");
}
else
Console.WriteLine("是素数");
12.一个名为Circle类包含一个字段(半径),两个方法,一个方法求面积,另一个方法求周长,并在Main函数中传递相应的数据,来显示他的周长和面积
class Circle
{
public double sq(double r)
{
return Math.PI * r * r;
}
public double zc(double r)
{
return 2 * Math.PI * r;
}
}
class Program
{
static void Main(string[] args)
{
Circle cir = new Circle();
Console.WriteLine("sq={0},zc={1}",cir.sq(3),cir.zc(3));
}
}
13.设计一个形状为figure,字段的个数和构造函数的设计自己考虑,另外设计一个虚方法
设计一个圆的类Circle,继承figure类,并重写求面积的方法
设计完成后在Main函数中定义square和circle的实例,并调用求面积的方法,输出他们的面积
class Figure
{
public virtual double sq(int a,int b)
{
return a + b;
}
public virtual double cq(double r)
{
return r;
}
}
class Circle:Figure
{
public override double cq(double r)
{
return Math.PI * r * r;
}
}
class Square:Figure
{
public override double sq(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
Circle cir = new Circle();
Console.WriteLine("cq={0}",cir.cq(3));
Square sq = new Square();
Console.WriteLine("sq={0}", sq.sq(1,2));
}
}
14.设计一个类,包含两个字段(double类型)和一个构造函数,初始化这两个字段,另外还包含一个虚方法(输出这两个字段的和)
构造另外一个类,此类继承设计的那个类要求重写虚方法(输出这两个数的积)
在Main函数中定义上面类的实例,传入适当的参数,使得他们在屏幕上显示。
class FatherClass
{
public double a;
public double b;
public FatherClass()
{
this.a = a; this.b = b;
}
public virtual double Sum(double a,double b)
{
return a + b;
}
}
class SonClass:FatherClass
{
//public SonClass() : base() { }构造函数
public override double Sum(double a, double b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
FatherClass fs = new FatherClass();
SonClass ss = new SonClass();
Console.WriteLine(ss.Sum(2.2,3.3));
}
}
two
1. 输入一个十进制的数,输出它的十六进制数。
Console.WriteLine("请输入一个十进制的数:");
int i = int.Parse(Console.ReadLine());
Console.WriteLine("它的十六进制数是:{0:x}", i);
2. 输入一个英文小写字母,将它转换成大写字母。
//第一种方法:
Console.WriteLine("请输入一个英文字母:");
char a = Convert.ToChar(Console.ReadLine());
if (a > 'a' && a < 'Z')
{
Console.WriteLine(a.ToString());
}
else
Console.WriteLine(a.ToString().ToUpper());
// 第三种方法:(int类型的转换)
int c = Convert.ToInt32(Console.Read());
char x = (char)c;
Console.WriteLine(x.ToString().ToUpper());
//第二种方法(char类型)
Console.WriteLine("请输入一个英文字母:");
char a = Convert.ToChar(Console.Read());
if (a > 65 && a < 90)
{
Console.WriteLine(a);
}
else
{
Console.WriteLine(a.ToString().ToUpper());
}
//第四种方法:(string类型)
Console.WriteLine("请输入一个英文小写字母:");
string a = Convert.ToString(Console.ReadLine());
Console.Write(a.ToUpper());
3. 输入三个整数,求三个数的和。
Console.WriteLine("请输入三个整数:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
//方法一:
int sum = a + b + c;
Console.WriteLine("这三个整数的和是:{0}", sum);
//方法二:
Console.WriteLine("这三个整数的和是:{0}", a + b + c);
4. 输入一个DOUBLE类型数,输出它的整数部分。
Console.WriteLine("输入一个DOUBLE类型数:");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("{0:#}", a);
5. 输入两个数输出其中最大的一个。(使用三目运算符)。
Console.WriteLine("请输入两个数:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int max;
max = a > b ? a : b;
Console.WriteLine("max={0}", max);
6. 求1+2+3+……+10的和。
int a;
int sum = 0;
for (a = 1; a <= 10; sum += a++) ;
Console.WriteLine("sum={0}", sum);
7. 输入一个圆的直径,求它的周长和面积。
Console.WriteLine("请输入一个圆的直径:");
double d = Convert.ToDouble(Console.ReadLine());
double c = 2 * Math.PI * d / 2;
double s = Math.PI * d / 2 * d / 2;
Console.WriteLine("周长={0},面积={1}", c, s);
8. 输入一个数,判断它是否同时被3和5整除。
Console.WriteLine("请输入一个数:");
int s = int.Parse(Console.ReadLine());
if (s % 3 == 0 && s % 5 == 0)
{
Console.WriteLine("{0}能够同时被3和5整除", s);
}
else
Console.WriteLine("{0}不能够同时被3和5整除", s);
9. 说出console.write()与console.writeline();console.read()与console.readline();const与readonly ;%与/的区别。
/* console.write()与console.writeline();
* console.writeLine()换行,而console.write()不换行
* console.read()与console.readline();
* console.readline()是读取输入的一行的数据,而console.read()是读取首字母的ASC的值
* const与readonly
* const 编译常量 ,是静态的 ,不能和static一起用 ;readonly 执行常量
* %与/的区别*
* %是取余,/是取整
* /
10. 打印出: NAME SEX ADDRESS
SA 16 BEIJING
TT 20 WUHAN
Console.WriteLine("NAME\tSEX\tADDRESS");
Console.WriteLine("SA\t16\tBEUING");
Console.WriteLine("TT\t20\tWUHAN");
three
+ View Code
four
1. 输入一个邮箱地址,如果正确则显示success否则显示error(正确的邮箱地址包含@)。
Console.WriteLine("请输入一个邮箱地址:");
bool b =true;
while (b)
{
string str = Convert.ToString(Console.ReadLine());
if (str.Contains("@") && str.Contains(".com"))
{
Console.WriteLine("Success");
}
else
Console.WriteLine("error");
}
2. 输入一个小写字母,要求输出它的大写字母。
Console.Write("请输入一个小写字母:");
string a = Convert.ToString(Console.ReadLine());
Console.WriteLine(a.ToUpper());
//字符串大小写相互转换
//String类型
while (true)
{
string str = Convert.ToString(Console.ReadLine());
char ch = char.Parse(str);
if (ch >= 'a' && ch <= 'z')
{
Console.WriteLine(str.ToUpper());
}
else
Console.WriteLine(str.ToLower());
}
============================================================================================
//Char类型
while (true)
{
char ch = char.Parse(Console.ReadLine());
//if (ch >= 'a' && ch <= 'z')
if (ch >= 97 && ch <= 122)
{
Console.WriteLine((char)(ch - 32));
}
else
Console.WriteLine((char)(ch + 32));
}
3. 输入一个5位数整数,将其倒向输出。(例如:输入12345,输出 54321).
while (true)
{
Console.WriteLine("输入整数:");
int a = Convert.ToInt32(Console.ReadLine());
int length = a.ToString().Length;
int[] b = new int[length];
for (int i = 0; i <= length - 1; i++)
{
int num = length - i;
int num1 = Convert.ToInt32(Math.Pow(10, num));
int num2 = Convert.ToInt32(Math.Pow(10, num - 1));
int cout1 = a % num1;
b[i] = cout1 / num2;
//Console.Write(b[num - 1]);
}
==================================================================
方法一:使用Array类的Reverse()方法
Array.Reverse(b);
foreach (int m in b)
{
Console.Write(m);
}
Console.Write("\n");
}
==================================================================
方法二:使用堆栈
Stack numbers = new Stack();
//填充堆栈
foreach (int number in b)
{
numbers.Push(number);
//Console.Write(number);
}
//遍历堆栈
foreach (int number in numbers)
{
//Console.Write(number);
}
//清空堆栈
while (numbers.Count != 0)
{
int number = (int)numbers.Pop();
Console.Write(number);
}
Console.Write("\n");
==================================================================
输入几个数按照1,2,3的格式输出(简单的说就是每两个数字之间必须用逗号隔开)
string str = Convert.ToString(Console.ReadLine());
string[] arraystr = str.Split(',');//数组的动态存储,用Realdine读取。
Array.Reverse(arraystr);//逆着输出
foreach (string i in arraystr)
{
Console.Write("{0}\0",i);
}
4. 从界面输入5个数, 然后根据这5个数,输出指定数目的”*”,如有一个数为5,则输出”*****”.
while (true)
{
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.Write("*");
}
Console.Write("\n");
}
5. 将如下字符串以”.”分割成3个子字符串. ” www.softeem.com”。
string a = "www.softeem.com";
string[] str = a.Split('.');
Console.WriteLine("{0}\0{1}\0{2}",str[0],str[1],str[2]);
算法:不管输入多长如例子a.b.c.e.f.g.h.i 输出为 a b c d e f g h有点则分割,没点就输出
bool b = true;
while (b)
{
Console.Write("输入数据:");
string a = Convert.ToString(Console.ReadLine());
if (a.Contains("."))
{
Console.WriteLine(a.Replace('.',' '));//此处用到了替换,直接将‘.’替换为 '';
}
else
Console.WriteLine(a);
}
6. 1 2 3 5 3 5 7 9
4 5 6 2 4 0 3 0
5 6 2 3 + 2 3 0 8 =?
8 5 3 1 9 7 5 1
int[,] num1 = { { 1, 2, 3, 5 }, { 4, 5, 6, 2 }, { 5, 6, 2, 3 }, { 8, 5, 3, 1 } };
int[,] num2 = { { 3, 5, 7, 9 }, { 4, 0, 3, 0 }, { 2, 3, 0, 8 }, { 9, 7, 5, 1 } };
int[,] sum = new int[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
sum[i, j] = num1[i, j] + num2[i, j];
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0}\t", sum[i, j]);
}
Console.Write("\n");
}
}
7. 2 4 9 1 2
1 3 5 * 4 5 =?
1 0
int[,] num1 = new int[2, 3] { { 2, 4, 9 }, { 1, 3, 5 } };
int[,] num2 = new int[3, 2] { { 1, 2 }, { 4, 5 }, { 1, 0 } };
int[,] sum = new int[2, 3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i, j] = num1[i, j] * num2[j, i];
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0}\t", sum[i, j]);
}
Console.Write("\n");
}
}
8 输入a,b,c的值。求 ax*x+bx+c=0的根。
while (true)
{
Console.WriteLine("请输入三个数:");
double a = Convert.ToInt32(Console.ReadLine());
double b = Convert.ToInt32(Console.ReadLine());
double c = Convert.ToInt32(Console.ReadLine());
double d = b * b - 4 * a * c;
double x1 = (Math.Sqrt(d) - b) / (2 * a);
double x2 = (-b - (Math.Sqrt(d))) / (2 * a);
if (b * b - 4 * a * c < 0)
{
Console.WriteLine("该方程没有根");
}
else if (x1==x2)
{
Console.WriteLine("该方程的根是:x1=x2={0}", x1);
}
else
Console.WriteLine("该方程的根是:x1={0},x2={1}", x1, x2);
}
9 输入5个数,将其重小到大排列输出。(使用冒泡)
非冒泡排序
Console.WriteLine("请输入五个数");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int d = int.Parse(Console.ReadLine());
int e = int.Parse(Console.ReadLine());
int min1 = a < b ? (a < c ? a : c) : (b < c ? b : c); //比较三个数的大小
int min2 = d < e ? d : e;
int min = min1 < min2 ? min1 : min2;
Console.WriteLine(min);
冒泡排序:
Console.Write("请按1,2,3的格式输入几个数字:");
string str = Convert.ToString(Console.ReadLine());
string[] arraystr = str.Split(',');
int arraylength = arraystr.Length;
int[] num = new int[arraylength];
int count = arraylength - 1;
for (int i = 0; i <arraylength; i++)
{
num[i] = int.Parse(arraystr[i]);
}
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i; j++)
{
if (num[j] > num[j + 1])
{
int temp;
temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
foreach (int i in num)
{
Console.Write("{0}\0",i);
}
10 输入一个数n,求n!。(使用递归)
bool b = true;
while (b)
{
Console.Write("输入数:");
int n = Convert.ToInt32(Console.ReadLine());
int sum = 1;
for (int i = 1; i <= n; i++)
{
sum *= i;
}
Console.WriteLine(sum);
}
}
five
1. 创建一个Employ类, 实现一个计算全年工资的方法.
class Employ
{
public int Getyearmoney(int monthmoney)
{
return 12 * monthmoney;
}
static void Main(string[] args)
{
Employ employ = new Employ();
Console.Write("请输入月工资:");
int monthmoney = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("年工资为:{0}",employ.Getyearmoney(monthmoney));
}
}
2. 定义一个方法, 既可以求2个整数的积,也可以求3或4个整数的积.
public static int Cj(params int[] nums)
{
int Mul = 1;
foreach (int num in nums)
{
Mul *= num;
}
return Mul;
}
static void Main(string[] args)
{
///将数字按照字符串的格式输出
Console.WriteLine("请输入数据:");
string a = Convert.ToString(Console.ReadLine());
string[] str = a.Split(',');
int[] num = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
num[i]=int.Parse(str[i]);//将string[]转换为int[]类型;
}
int c = 0;
foreach (int i in num)//遍历数组乘积累积
{
c = Cj(num);
}
Console.WriteLine(c);
}
3. 编写一个方法,求两点(x1,y1)与(x2,y2)之间的距离(用户自己输入点的坐标);
class Program
{
public int Mul(int x1, int y1, int x2, int y2)
{
return Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
}
static void Main(string[] args)
{
Program pr = new Program();
Console.Write("第一个点的横坐标:");
int x1 =Convert.ToInt32(Console.ReadLine());
Console.Write("第一个点的纵坐标:");
int y1 =Convert.ToInt32(Console.ReadLine());
Console.Write("第二个点的横坐标:");
int x2 =Convert.ToInt32(Console.ReadLine());
Console.Write("第二个点的纵坐标:");
int y2 =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("这两个点之间的距离是:{0}",pr.Mul(x1, y1, x2, y2));
}
}
class Program
{
/// <summary>
/// 求两点间的距离
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public int Fartwopoint(int[,] point)
{
Console.Write("第一个点的横坐标:");
int x1 = int.Parse(Console.ReadLine());
Console.Write("第一个点的纵坐标:");
int y1 = int.Parse(Console.ReadLine());
Console.Write("第二个点的横坐标:");
int x2 = int.Parse(Console.ReadLine());
Console.Write("第二个点的纵坐标:");
int y2 = int.Parse(Console.ReadLine());
int[,] a = new int[2, 2] { { x1, y1 }, { x2, y2 } };
double d = Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
int c = Convert.ToInt32(Math.Sqrt(d));
return c;
}
static void Main(string[] args)
{
Program pr = new Program();
int[,] point = new int[2, 2];
Console.WriteLine(pr.Fartwopoint(point));
}
}
方法二:
class Program
{
public int Mul(int x1, int y1, int x2, int y2)
{
return Convert.ToInt32(Math.Sqrt(Convert.ToDouble((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
}
static void Main(string[] args)
{
Program pr = new Program();
Console.Write("第一个点的横坐标:");
int x1 =Convert.ToInt32(Console.ReadLine());
Console.Write("第一个点的纵坐标:");
int y1 =Convert.ToInt32(Console.ReadLine());
Console.Write("第二个点的横坐标:");
int x2 =Convert.ToInt32(Console.ReadLine());
Console.Write("第二个点的纵坐标:");
int y2 =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("这两个点之间的距离是:{0}",pr.Mul(x1, y1, x2, y2));
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方法三:
class Point
{
private int x1;
private int y1;
private int x2;
private int y2;
public int Jl(int x1,int y1,int x2,int y2)
{
int z = Convert.ToInt32(Math.Sqrt(Convert.ToInt32((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))));
return z;
}
static void Main(string[] args)
{
Point poi = new Point();
Console.WriteLine(poi.Jl(poi.x1,poi.x2,poi.y1,poi.y2));
}
}
4. 创建一个Student类, 定义5个字段和2个方法,上述类里, 定义两个同名方法GetAge, 一个返回年龄,一个根据传入的出生年月,返回年龄。
class Student
{
private int id;
private int age;
private string name;
private string sex;
private DateTime birthday;
public int GetAge(int age)
{
return age;
}
public int GetAge(DateTime birthday)
{
return DateTime.Now.Year - birthday.Year + 1;
}
static void Main(string[] args)
{
Console.WriteLine("请输入出生日期:");
string stubirthday = Convert.ToString(Console.ReadLine());
Student stu = new Student();
stu.birthday = Convert.ToDateTime(stubirthday);
Console.WriteLine("年龄:{0}",stu.GetAge(stu.birthday));
}
}
5. 编写一个方法,输入一个整数,然后把整数一反序返回(比如输入8976,返回6798);
8976 = 8 x 1000 +9 x 100 +7 x 10 +6 x 1;
6798 = 6 x 1000 +7 x 100 +9 x10 +8 x 1;
1000 = Math.Pow (10, a.Length); a-length --;
6 = a %10 ,7 = a /1000 - a/100,9 = a/100 -a/10
6. 定义一个类,完成一个方法,以数字表示的金额转换为以中文表示的金额, 如下24000,098.23贰千肆佰万零玖拾捌元贰角八分,1908.20 壹千玖佰零捌元贰角
7. static ,viod 是什么意思,get与set,ref与out,public与private的区别是什么。
Static静态类
Void 无返回值
get 可读,set 可写
ref开始必须赋值
out开始不须直接赋值
public 公共可访问的类
private私有的仅在内部类中使用
six
1. 封装一个N!,当输入一个数时,输出它的阶层。
sealed class Data//密封类
{
public int N(int a)
{
int sum =1;
for(int i =1;i<=a;i++)
{
sum *= i;
}
return sum;
}
}
private void button1_Click(object sender, EventArgs e)
{
Data dt = new Data();
MessageBox.Show(dt.N(3).ToString());
}
2. 定义一个索引,访问其相关的内容。
==定义Student 和Students 两个类
public class Student
{
public Student(int id, string name, int age, string sex)
{
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
public class Students
{
Student[] stu = new Student[3];
public Students()
{
Student stu1 = new Student(1,"a",23,"men");
Student stu2 = new Student (2,"b",23,"women");
Student stu3 =new Student (3,"c",21,"men");
stu[0]=stu1;
stu[1]=stu2;
stu[2]= stu3;
}
public Student this[int index]
{
get
{
return stu[index];
}
set
{
stu[index] =value;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Students stu = new Students();
MessageBox.Show(stu[0].Name+" "+stu[1].Sex+" "+stu[2].Id);
}
3. 定义一个抽象类,并在子类中实现抽象的方法.
//定义抽象类Abtr
abstract class Abtr
{
public abstract int Num(int a,int b,int c);// 抽象类的抽象方法
public virtual int Max(int a,int b,int c) //抽象类的虚方法
{
int max1 = Math.Max(a, b);
int max = Math.Max(max1, c);
return max;
}
}
//子类是抽象类
abstract class CAbtr : Abtr
{
public abstract override int Num(int a, int b, int c);
}
//子类不是抽象类时候
class CAbtr : Abtr
{
public override int Num(int a, int b, int c)
{
return Convert.ToInt32(Math.Sqrt(Convert.ToDouble(a * b * c)));
}
public override int Max(int a, int b, int c)
{
int max1 = Math.Max(a, b);
int max = Math.Max(max1, c);
return 100*max;
}
}
4. 有三角形,矩形,正方形,梯形,圆,椭圆,分别求出各自的周长,面积(abstract).
abstract class Refent
{
public abstract int Sanjiaozc(int d,int h,int a,int b);
public abstract int Sanjiaomj(int d, int h,int a,int b);
public abstract int Juxizc(int l, int h);
public abstract int Juximj(int l, int h);
public abstract int Zhengzc(int a);
public abstract int Zhengmj(int a);
public abstract int Tixingzc(int a, int b , int h,int c,int d);
public abstract int Tixingmj(int a, int b, int h,int c,int d);
public abstract int Cirzc(int r);
public abstract int Cirmj(int r);
}
class CRent:Refent
{
int zc; int mj;
public override int Sanjiaozc(int d,int h,int a,int b)
{
zc = d+ a + b;
return zc;
}
public override int Sanjiaomj(int d, int h, int a, int b)
{
mj = d * h / 2;
return mj;
}
public override int Juxizc(int l, int h)
{
zc = 2*(l + h);
return zc;
}
public override int Juximj(int l, int h)
{
mj = 2 * l * h;
return mj;
}
public override int Zhengzc(int a)
{
zc = 4 * a;
return zc;
}
public override int Zhengmj(int a)
{
mj = a * a;
return mj;
}
public override int Tixingzc(int a, int b, int h, int c, int d)
{
zc = a + b +c +d;
return mj;
}
public override int Tixingmj(int a, int b, int h, int c, int d)
{
mj = (a + b) * h / 2;
return mj;
}
public override int Cirmj(int r)
{
mj = Convert.ToInt32(Math.PI * r * r);
return mj;
}
public override int Cirzc(int r)
{
zc = Convert.ToInt32(2 * Math.PI * r);
return zc;
}
}
5. 分析:为什么String类不能被继承.
string类是密封类
6. 定义一个接口,接口必须含有一个方法, 用一个实现类去实现他.
interface Inte
{
int GetAge(string date);
}
public class Employee:Inte
{
public int GetAge(string date)
{
DateTime dt = DateTime.Parse(date);
int age =dt.Year - DateTime.Now.Year;
return age;
}
}
7. 说明接口和抽象类的区别.
1. 接口不能有构造函数,抽象类有构造函数
2. 接口允许方法的实现,抽象方法是不能被实现的
3. 接口里面不须override来重写,抽象类一定要override重写
8. 如果从TextBox输入一个参数, 就求圆的周长, 两个参数就求矩形的周长,三个参数就求三角形的周长.
***对于params的用法还不是很了解?ArrayList, 还有HashTable
class ZC
{
public int C(params int[] a)
{
int sum = 0;
foreach (int i in a)
{
if (a.Length == 1)
{
sum += Convert.ToInt32(2 * Math.PI * i);
}
if (a.Length == 2)
{
sum += 2* i;
}
if (a.Length==3)
{
sum += i;
}
}
return sum;
}
}
WinFrom中的代码:
private void button1_Click(object sender, EventArgs e)
{
ZC zc = new ZC();
string s = textBox1.Text.Trim();
string[] str = s.Split(',');
int[] a = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
a[i] = int.Parse(str[i]);
}
MessageBox.Show(zc.C(a).ToString());
}
9. 说明重载与重写的区别。
1.重载函数之间的参数类型或个数是不一样的.重载方法要求在同一个类中.
(解释:例如原来有一个构造函数
public Student(){},
现在有重载了这个构造函数
public Student(int age ,int id,string name)
{
this.age = age ;
this.name = name ;
this.id = id;
})
2.重写要求返回类型,函数名,参数都是一样的,重写的方法在不同类中.
10. 完成一个小型的计算器,不熟悉的功能可以不完成
11.
12. Public private internal protected的使用范围。
大到小:Public protected internal private
Public 所有类,protected 子父类访问internal当前项目,private当前类
seven
1.定义一个委托,委托一个去借东西的方法Send,如果借到了返回GOOD!,否则NO!
class Borrow
{
public delegate string Send();
public string Br(Send send)
{
return send();
}
public string OK()
{
return "GOOD!";
}
public string NO()
{
return "NO!";
}
}
调用:
Borrow b = new Borrow();
MessageBox.Show(b.Br(b.OK));
2.定义一个事件,当我激发它时,返回一个结果给我.
class Event1
{
public delegate string Eve();
public event Eve eve; //事件
public string Trigger()
{
if (eve != null)
return eve();
else
return "";
}
public string A1()
{
return "A1";
}
public string A2()
{
return "A2";
}
}
调用:
Event1 event1 = new Event1();
event1.eve += event1.A1;
MessageBox.Show(event1.Trigger());
3.定义一个拿100元钱去买东西的委托,书的价格是58元,本的价格是20元,返回剩余的钱.
class Event1
{
public delegate int Eve(int sum);
public event Eve eve; //事件
public int Trigger(int sum)
{
if (eve != null)
return eve(sum);
else
return 0;
}
public int Book(int sum)
{
return sum-=58;
}
public int Text(int sum)
{
return sum-=20;
}
}
调用:
Event1 event1 = new Event1();
int sum = 100;
event1.eve += event1.Book;
sum = event1.Trigger(sum);
event1.eve += event1.Text;
sum = event1.Trigger(sum);
MessageBox.Show(sum.ToString());
4.说出委托与事件的区别.
事件属于委托,委托不属于事件.事件是委托的一个变量.
eight
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Max(1, 4, 6, 8, 3).ToString());
}
public int Max(params int[] array)
{
int max = Int32.MinValue;
foreach (int i in array)
{
max = Math.Max(i, max);
}
return max;
}
private void button2_Click(object sender, EventArgs e)
{
string str = "";
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 2*i+1; j++)
{
str += "* ";
}
str += "\r\n";
}
for (int i = 0; i < 3; i++)
{
for (int j = 5; j > 2*i; j--)
{
str += "* ";
}
str += "\r\n";
}
richTextBox1.Text = str;
}
private void button3_Click(object sender, EventArgs e)
{
DateTime date1 = DateTime.Parse("1998-1-1 00:00:00");
DateTime date2 = DateTime.Parse("2009-3-13 00:00:00");
TimeSpan time = date2 - date1;
int days = time.Days+1;
if (days % 5 == 0)
{
MessageBox.Show("晒网");
}
else
{
if (days % 5 > 3)
{
MessageBox.Show("晒网");
}
else
{
MessageBox.Show("打鱼");
}
}
}
/// <summary>
/// 求200小孩。。。。。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
int length = 200;
int[] array = new int[length];
for (int i = 0; i < length; i++)
{
array[i] = i + 1;
}
string str = null;
for (int i = 1; i <= length; i++)
{
str += i.ToString() + ",";
}
Out(str, array);
}
public void Out(string str,int []array)
{
if (array.Length == 3)
{
MessageBox.Show(array[1].ToString());
}
else
{
int lit = array.Length % 4;
string st = "";
for (int i = lit; i>0; i--)
{
st += array[array.Length - i].ToString()+",";
}
for (int i = 1; i <= array.Length; i++)
{
if (i % 4 == 0)
{
str=str.Replace(","+array[i - 1].ToString() + ",", ",");
}
}
int length = str.Length;
if (st == "")
{
str = str;
}
else
{
str = str.Replace(st, "");
str = st + str;
}
string[] arraystring = str.Split(',');
int[] arrayint = new int[arraystring.Length-1];
for (int i = 0; i <arraystring.Length-1; i++)
{
arrayint[i] = int.Parse(arraystring[i]);
}
Out(str, arrayint);
}
}
private void button5_Click(object sender, EventArgs e)
{
int sum = 0;
Random rand = new Random();
for (int i = 0; i < 100; i++)
{
int x = rand.Next(65, 91);
char y = Convert.ToChar(x);
if (y == 'A' || y == 'O' || y == 'E' || y == 'I' || y == 'U')
{
sum++;
}
}
MessageBox.Show(sum.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
string str = "2,";
for (int i = 3; i <= 100; i++)
{
int j;
for ( j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}
}
if (i==j)
{
str += i.ToString()+",";
}
}
MessageBox.Show(str);
}
private void button7_Click(object sender, EventArgs e)
{
int i = 27863654;
string str = Convert.ToString(i, 2);
string sumstr = str.Replace("0", "");
MessageBox.Show(sumstr.Length.ToString());
}
private void button8_Click(object sender, EventArgs e)
{
string str = "";
for (int i = 100; i < 1000; i++)
{
int a = i % 10;//个位
int b = (i / 10) % 10;//十位
int c = i / 100;//百位
int sum = a * a * a + b * b * b + c * c * c;
if (sum == i)
{
str += i.ToString() + ",";
}
}
MessageBox.Show(str);
}
private void button9_Click(object sender, EventArgs e)
{
int sum = 0;
string str = "";
for (int i = 2; i < 1000; i++)
{
for (int j = 1; j < i; j++)
{
if (i % j == 0)
{
sum += j;
}
}
if (sum == i)
{
str += i.ToString() + ",";
}
sum = 0;
}
MessageBox.Show(str);
}
private void button10_Click(object sender, EventArgs e)
{
int sum = 0;
int length;
for (int i = 1; i < 9; i++)
{
sum += Convert.ToInt32( 200 * Math.Pow(2, i));
}
sum += 200;
length = 100 + Convert.ToInt32(sum / Math.Pow(2, 9));
MessageBox.Show(length.ToString());
}
private void button11_Click(object sender, EventArgs e)
{
int n = 225;
int i = 2;
string str = "";
while (n != 1)
{
while (n % i == 0)
{
if (n / i !=1)
{
str += i.ToString()+"*";
}
else
{
str += i.ToString();
}
n = n / i;
}
i++;
}
MessageBox.Show(str);
}
private void button12_Click(object sender, EventArgs e)
{
int max = 20;
int min = 5;
//最大公约数
string strmax = "";
//for (int i = min; i >0; i--)
//{
// if (max % i == 0 && min % i == 0)
// {
// strmax = i.ToString();
// break;
// }
//}
//zuixiaogongbeishu
for (int i = max; i <= max*min; i++)
{
if (i%max==0 && i%min==0)
{
strmax += i.ToString() + ",";
break;
}
}
MessageBox.Show(strmax);
}
private void button13_Click(object sender, EventArgs e)
{
int[,] array = new int[20, 2];
array[0, 0] = 2;
array[0, 1] = 1;
for (int i = 1; i < 20; i++)
{
for (int j = 0; j < 2; j++)
{
if (j == 0)
{
array[i, j] = array[i - 1, j] + array[i - 1, j + 1];
}
else
{
array[i, j] = array[i - 1, j - 1];
}
}
}
double dmin = 1;
for (int i = 0; i < 20; i++)
{
dmin *= array[i, 1];
}
double bsum = 0;
for (int i = 0; i < 20; i++)
{
bsum += (dmin / array[i, 1]) * array[i, 0];
}
MessageBox.Show(bsum.ToString()+"/"+dmin.ToString());
//double min=Math.Min(dmin,bsum);
//double maxcount = 0;
//for (double i = min; i > 0; i--)
//{
// if (dmin % i == 0 && bsum % i == 0)
// {
// maxcount = i;
// break;
// }
//}
//string str = (bsum / maxcount).ToString() + "//" + (dmin / maxcount).ToString();
//MessageBox.Show(str);
}
private void button14_Click(object sender, EventArgs e)
{
Humen student1 = new Humen();
student1.Age = 10;
Humen student2 = new Humen();
student2.Age = student1.Age + 2;
}
1.获取以下html片段的第二个<span />元素。
<div id=”d1”><span>1</span><span>2</span></div>
2.说出以下表达式值:
typeof(NaN)
typeof(Infinity)
typeof(null)
typeof(undefined)
Object.constructor
Function.constructor
3.说出下面这个函数的作用
function(destination, source) {
for (property in source) {
destination[property] = source[property];
}
return destination;
};
4.说出下面代码的运行结果
var a = "123abc";
alert(typeof(a++));
alert(a); 数据库
1.列举ADO.NET主要对象,并阐述其用途。
DataSet(离线数据集,相当于内存中的一张表),Connection(由于数据库连接操作),Command(由于数据库更新操作),DataAdaper(数据库离线状态下的查询及更新操作),DataReader(只读向前的数据查询对象)
2.DataReader和DataSet有什么区别?
DataReader:连线操作对象,只读向前 大量数据
DataSet:离线操作对象,相当于内存中的一张表,少量数据
3.什么是索引?有什么作用?
索引用于标示数据在内存的地址
索引是一个单独的、物理的数据库结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。
4.Sql Server2000和Sql Server2005编写、调试存储过程有哪些不同?
5.如何设计树状数据结构?
通过设置标号和父标号,做成动态链表形式。
6.有以下3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
a.使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名。
Select S#,SN from s
Where S# in
(select S# from SC where S.S#=SC.S# and C# in
(select C# from C where CN=’税收基础’
)
)
b.使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位。
Select SN ,SD from s where S# in(select S#from SC where C#=’C2’)
c.使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位。
Select SN ,SD from s where S# in(select S#from SC where C# is not C2)
d.使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位。
e.查询选修了课程的学员人数。
Select count(*) from S where S# in (select S#from SC )
f.查询选修课程超过5门的学员学号和所属单位。
Select SN,SD from S JOIN (SELECT SC.S# FROM SC GROUP BYS# having count(*)>5) C ON S.S# =C.S#