1.題目要求:
用C#設計一個簡單的
Windows應用程序,在文本框中輸入兩個點的坐標值,單擊“計算“時顯示兩點之間的距離。如圖所示:要求定義一個 Point類,包括
(1)兩個私有字段表示兩個坐標值。
(2)一個構造函數通過傳入的參數對坐標值初始化。
(3)兩個只讀屬性對坐標值的讀取。
(4)一個方法包含一個Point類對象作為形參,計算該對象和自己的距離。
2.來吧展示,代碼如下:
using System; using System.Windows.Forms; namespace Distance_between_two_points { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { MessageBox.Show("歡迎使用兩點距離計算器!"); } private void button1_Click(object sender, EventArgs e) { Point p1 = new Point(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)); Point p2 = new Point(Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text)); richTextBox1.Text = p1.Distance(p2).ToString(); } private void button2_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; richTextBox1.Text = ""; } } class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } } public int Y { get { return y; } } public double Distance(Point p) { return System.Math.Sqrt((this.X - p.X) * (this.X - p.X) + (this.Y - p.Y) * (this.Y - p.Y)); } } }
3.運行如下:
我是小關,關注我,帶你從初級入門編程
希望能幫到大家,問你們要一個贊,你們會給嗎,謝謝大家
版權聲明:本文版權歸作者(@攻城獅小關)和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
大家寫文都不容易,請尊重勞動成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/