问题及代码:
运行结果:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:days.cpp
*作 者:单昕昕
*完成日期:2015年2月7日
*版 本 号:v1.0
*
*问题描述:求输入两点间的距离。
*程序输入:两点。
*程序输出:两点间的距离。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class Program
{
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public double distance(Point p)
{
return Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
}
class Test
{
static void Main()
{
int a, b, c, d;
Console.WriteLine("请输入p1的坐标");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
Point p1 = new Point(a, b);
Console.WriteLine("请输入p2的坐标");
c = int.Parse(Console.ReadLine());
d = int.Parse(Console.ReadLine());
Point p2 = new Point(c, d);
double distance_ = p1.distance(p2);
Console.Write("点p1(" + p1.x + "," + p1.y + ")");
Console.Write("和点p2(" + p1.x + "," + p1.y + ")");
Console.WriteLine("间的距离=" + distance_);
Console.ReadLine();
}
}
}
}
运行结果:
对象的创建与使用。
声明和使用方法。
学习心得:
类名 对象名=new 类名(参数表)//创建对象
对象名 .属性 //访问对象属性
对象.方法名(参数列表)//调用对象方法