C#练习(1~8)源代码下载请到http://download.youkuaiyun.com/detail/hsttmht/3751088
引用请注明http://blog.youkuaiyun.com/hsttmht
1、定义一个类矩形,定义四个double类型的变量,分别表示矩形的左下角的坐标和右上角的坐标,在定义一个无参构造函数初始化所有变量为0;定义一个有参构造函数对这四个变量进行赋值。另外定义一个求面积的和一个求周长的公有方法,在Main()方法中从控制台输入四个坐标点,然后调用方法输出面积和周长
2、构建一个类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数,另外在该类中提供一个方法计算两个点的直线距离,传入参数为Point类实例,然后设计一个测试类来对Point类进行使用。
3、实验要求
定义学生类,
字段:学号(字符串类型)
姓名(字符串类型)
性别(枚举类型)
年龄(整形类型)
学生个数(静态整形 用于统计学生实例个数 )
函数:
构造函数:参数为前四个字段;
静态函数:无参数,返回值为整形即学生实例个数
在Main函数中使用定义的构造函数实例化4个学生类对象,在创建每一个学生实例后立即输出学生信息,并调用静态函数输出学生实例个数,注意静态字段、静态函数的使用。
using System;
using System.Collections.Generic;
namespace PointNameSpace
{
public class Point
{
public double X1;
public double Y1;
public double X2;
public double Y2;
public Point()
{
X1 = 0.0;
Y1 = 0.0;
X2 = 0.0;
Y2 = 0.0;
}
public Point(double x1, double y1)
{
X1 = x1;
Y1 = y1;
//X2 = x2;
//Y2 = y2;
}
public static double zhouchang(Point A, Point B)
{
return 2*((A.X2 - B.X1) + (A.Y2 - B.Y1));
}
public static double mianji(Point A, Point B)
{
return (A.X2 - B.X1)*(A.Y2 - B.Y1);
}
public static void Main(string[] args)
{
//第一个点
double x1, y1;
x1 = double.Parse(Console.ReadLine());
y1 = double.Parse(Console.ReadLine());
Point point1 = new Point(x1,y1);
//第二个点
double x2, y2;
x2 = double.Parse(Console.ReadLine());
y2 = double.Parse(Console.ReadLine());
Point point2 = new Point(x2,y2);
//传入
double f = Point.zhouchang(point1,point2);
Console.WriteLine("周长:{0}",f);
double a = Point.mianji(point1,point2);
Console.WriteLine("面积:{0}",a);
Console.ReadLine();
}
}
}

using System;
using System.Collections.Generic;
using System.Text;
namespace PointNameSpace
{
public class Point
{
public double X;
public double Y;
//两个公有的构造函数
public Point()
{
X = 0.0;
Y = 0.0;
}
public Point(double x, double y)
{
X = x;
Y = y;
}
//提供一个方法计算两个点的直线距离
public static double s(Point A, Point B)
{
return Math.Sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y));
}
//传入参数为Point类实例,然后设计一个测试类来对Point类进行使用。
public static void Main(string[] args)
{
//第一个点
double x1, y1;
x1 = double.Parse(Console.ReadLine());
y1 = double.Parse(Console.ReadLine());
Point point1 = new Point(x1,y1);
//第二个点
double x2, y2;
x2 = double.Parse(Console.ReadLine());
y2 = double.Parse(Console.ReadLine());
Point point2 = new Point(x2,y2);
//传入s
double f = Point.s(point1,point2);
Console.WriteLine(f);
Console.ReadLine ();
}
}
}
using System;
using System.Collections.Generic;
public class Student
{
string studentid;//学号(字符串类型)
string studentname;//姓名(字符串类型)
string studentsex;//性别(枚举类型)
public enum thestudentsex
{
boy,
girl
}
int studentage;//年龄(整形类型)
static int studentnum;//学生个数(静态整形 用于统计学生实例个数 )
public Student(string id,string name,thestudentsex sex,int age)//构造函数实例化4个学生类对象
{
studentid = id;
studentname = name;
if(sex==thestudentsex.girl)
{
studentsex = "女";
}
else
{
studentsex = "男";
}
studentnum++;//统计学生
studentage = age;
}
private static int GetStudentNum()
{
return studentnum;
}
static void Main(string[] args)
{
//实例化4个学生类对象
Student stu1 = new Student("0901110101", "A", thestudentsex.boy, 22);
Console.WriteLine("学号:" + stu1.studentid + " 姓名:" + stu1.studentname+" 性别:"+stu1.studentsex+" 年龄:"+stu1.studentage.ToString());
Student stu2 = new Student("0901110102", "B", thestudentsex.girl, 22);
Console.WriteLine("学号:" + stu2.studentid + " 姓名:" + stu2.studentname + " 性别:" + stu2.studentsex + " 年龄:" + stu2.studentage.ToString());
Student stu3 = new Student("0901110103", "C", thestudentsex.boy, 22);
Console.WriteLine("学号:" + stu3.studentid + " 姓名:" + stu3.studentname + " 性别:" + stu3.studentsex + " 年龄:" + stu3.studentage.ToString());
Student stu4 = new Student("0901110104", "D", thestudentsex.girl, 22);
Console.WriteLine("学号:" + stu4.studentid + " 姓名:" + stu4.studentname + " 性别:" + stu4.studentsex + " 年龄:" + stu4.studentage.ToString());
Console.WriteLine("定义了"+GetStudentNum().ToString()+"个学生");
Console.ReadLine();
}
}
本文提供了三个C#编程练习示例,包括矩形类定义及计算、点类构造函数与距离计算、学生类实例化及统计,适合初学者实践。
1044

被折叠的 条评论
为什么被折叠?



