【C#编程】两点距离计算

Attention!!如果定义的类名首字母大写,之后创建实例(对象)的时候都要首字母大写,反之统一为小写。如图




附完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 实验3
{
    public partial class Form1 : Form
    {
        public class point
        {
            private double x;
            private double y;
            public double X
            {
                get { return x; }
                set { x = value; }
            }
            public double Y
            {
                get { return y; }
                set { y = value; }
            }

            public static  double distance(Point p1,Point p2)
            {
                double result = 0;
                result = Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y)*(p1.Y - p2.Y));
                return result;
            }

        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            point p1 = new point();
            point p2 = new point();
            double value = 0;
            bool a=true;
            try
            {
                p1.X = double.Parse(textBox1.Text);
                p1.Y = double.Parse(textBox2.Text);
                p2.X = double.Parse(textBox3.Text);
                p2.Y = double.Parse(textBox4.Text);

                value = point.distance(p1, p2);
                label7.Text = value.ToString("0.00000");
            }

            catch (System.FormatException)
            {
                a = false;
                MessageBox.Show("输入字符串格式错误");
            }
            finally
            {
                if(a==true)
                MessageBox.Show("计算成功");
                else
                MessageBox.Show("计算失败");
            }


        }
        private void label7_Click(object sender, EventArgs e)
        {

        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值