class Rect
{
//長 和 寬
private double width = 0.0d;
private double height = 0.0d;
public Rect()
{
}
public Rect(double width, double height)
{
this.width = width;
this.height = height;
}
//寬對應的屬性
public double Width
{
get { return width; }
set { width = value; }
}
//長對應的屬性
public double Height
{
get { return height; }
set { height = value; }
}
//面積
public double Area()
{
return this.Height * this.width;
}
//周長
public double Perimeter()
{
return 2 * (this.Height + this.width);
}
}
//程序入口函數調用矩形類
class Program
{
static void Main(string[] args)
{
double width = 0.0d;
double height = 0.0d;
Console.WriteLine("請輸入矩形的長:");
string word1 = Console.ReadLine();
while(!VolidatorDouble(ref height, word1))
{
Console.WriteLine("輸入的格式不正確!請重新輸入矩形的長!");
word1 = Console.ReadLine();
}
Console.WriteLine("請輸入矩形的寬:");
string word2 = Console.ReadLine();
while (!VolidatorDouble(ref width, word2))
{
Console.WriteLine("輸入的格式不正確!請重新輸入矩形的寬!");
word2 = Console.ReadLine();
}
//創建矩形
Rect rect = new Rect();
rect.Height = height;
rect.Width = width;
Console.WriteLine("矩形的長:{0},寬:{1},面積:{2}",rect.Width,rect.Height,rect.Area());
Console.WriteLine("矩形的長:{0},寬:{1},周長:{2}", rect.Width, rect.Height, rect.Perimeter());
Console.ReadKey();
}
//驗證輸入的合法性
private static bool VolidatorDouble(ref double num,string word)
{
try
{
num = double.Parse(word);
return true;
}
catch
{
return false;
}
}
}