c#设计。使用类编制程序,该程序可以提示用户输入一个矩形的长度和宽度,然后显示矩形的边长和面积
展开全部
矩形类
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;
}
}
}
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;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询