第一次作业
1. 定义一个具有10元素的整形数组numbers,并为该数组的元素赋值为0、2、4、6……,最后将数组的值打印出来。
程序运行结果如下所示:
程序代码
static void Main(string[] args){int i;int[] numbers=new int[10];for (i =0; i <= 9; i++){numbers[i] = i*2;Console.WriteLine("numbers[{0}] is {1}", i, numbers[i]);}Console.ReadLine();}
2. 编写程序代码,实现将一个字符串的内容倒置的程序。要求首先将字符串转化为字符数组,对字符数组进行倒置操作,再转化为字符串输出。
程序运行结果如下所示:
提示:
string input = Console.ReadLine();char[] char1 = input.ToCharArray();//将字符串转化为字符数组char[] char2 = new char[char1.Length];//char1.Length可以取得字符数组char1的长度
程序代码
static void Main(string[] args){Console.WriteLine("请输入一个字符串:");string input = Console.ReadLine();char[] char1 = input.ToCharArray();char[] char2 = new char[char1.Length];Console.WriteLine("翻转后的结果是:");for (int i = 0; i < char1.Length; i++){char2[i] = char1[char1.Length - i - 1];//Console.Write("{0}", char2[i]);}string output;output = new string(char2);Console.WriteLine(output);Console.ReadLine(); }
第二次作业
1. 使用for循环语句,输出乘法表。
程序运行结果如下所示:
程序代码
static void Main(string[] args){for (int i = 1; i <= 9; i++){for (int k = 1; k <= 9; k++){if (k <= i){int m = k * i;Console.Write(“{0}*{1}={2} ”, i, k, m);//不换行}}Console.WriteLine(“”);//换行}Console.ReadLine();}
2. 学生姓名登记。
程序运行结果如下所示:
程序代码
static void Main(string[] args){int count=0;Console.Write(“请输入您要登记的学生人数:");try{count = Convert.ToInt32(Console.ReadLine()); }catch(Exception ex){Console.WriteLine(ex.Message);}string[] names = new string[count];for (int i = 0; i < count; i++){Console.Write("请输入第{0}个学生的姓名:", i + 1);names[i] = Console.ReadLine();}Console.WriteLine("已登记的学生如下:");foreach (string disp in names){Console.WriteLine("{0}", disp);}Console.ReadLine();}
练习
1. 构建一个类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数。另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。
提示:先定义两个变量来存储Point点的X,Y坐标;无参的构造函数将X,Y坐标赋为0;有参的构造函数将传入的参数分别赋给X,Y坐标。
程序运行结果如下所示:
程序代码
namespace ConsoleApplication3
{public class Point{public double x;public double y;public Point() //Point类不带参数的构造函数{x = 0;y = 0;}public Point(double pointx, double pointy) //Point类带参数的构造函数{x = pointx;y = pointy;}public static double Distance(double x0,double y0,double x1,double y1){double d = 0;d = Math.Sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));return d;}}class Program{static void Main(string[] args){Point point0 = new Point();Point point1 = new Point(3,4);double d = Point.Distance(point0.x,point0.y,point1.x,point1.y);Console.WriteLine("点({0},{1})与点({2},{3})之间的距离是:{4}",point0.x,point0.y,point1.x,point1.y,d);Console.ReadLine();}}
}
2. 构建一个类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数。另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。
提示:先定义两个变量来存储Point点的X,Y坐标;无参的构造函数将X,Y坐标赋为0;有参的构造函数将传入的参数分别赋给X,Y坐标。
程序代码方法一:
namespace ConsoleApplication3
{public class Point{public double x;public double y;public Point(){x = 0;y = 0;}public Point(double pointx, double pointy){x = pointx;y = pointy;}public static double Distance(double x0,double y0,double x1,double y1){double d = 0;d = Math.Sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));return d;}} class Program{static void Main(string[] args){double px = 0;double py = 0;double qx = 0;double qy = 0;Console.WriteLine("请输入第一个点的坐标:");try{px = Convert.ToDouble(Console.ReadLine());py = Convert.ToDouble(Console.ReadLine());}catch (Exception ex){Console.WriteLine(ex.Message+"所输入点的坐标将自动设置为(0,0)。");px = 0;py = 0;}Console.WriteLine("请输入第二个点的坐标:");try{qx = Convert.ToDouble(Console.ReadLine());qy = Convert.ToDouble(Console.ReadLine());}catch (Exception ex){Console.WriteLine(ex.Message + "所输入点的坐标将自动设置为(0,0)。");qx = 0;qy = 0;}Point point0 = new Point(px,py);Point point1 = new Point(qx,qy);double d = Point.Distance(point0.x,point0.y,point1.x,point1.y);Console.WriteLine("点({0},{1})与点({2},{3})之间的距离是:{4}",point0.x,point0.y,point1.x,point1.y,d);Console.ReadLine();}}
}
程序代码方法二:
namespace ConsoleApplication3
{public class Point{public double x;public double y;public Point(int i){Console.WriteLine("请输入第{0}个点的坐标:",i);try{x = Convert.ToDouble(Console.ReadLine());y = Convert.ToDouble(Console.ReadLine());}catch (Exception ex){Console.WriteLine(ex.Message + "所输入点的坐标将自动设置为(0,0)。");x = 0;y = 0;} }public Point(double pointx, double pointy){x = pointx;y = pointy;}public static double Distance(double x0, double y0, double x1, double y1){double d = 0;d = Math.Sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));return d;}}class Program{static void Main(string[] args){Point point0 = new Point(1);Point point1 = new Point(2);double d = Point.Distance(point0.x, point0.y, point1.x, point1.y);Console.WriteLine("点({0},{1})与点({2},{3})之间的距离是:{4}", point0.x, point0.y, point1.x, point1.y, d);Console.ReadLine();}}
}