我自己做了一个程序,可是总有错误,请大家帮我看看,如何修改!!
程序如下:
……
class Rectangle
{
private double width;
private double height;
public double Width
{
get { return width; }
set { width = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public double Area
{
get
{return Width * Height;}
}
public double Circumference
{
get{return (Width + Height) * 2;}
}
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
}
//键盘处理范例:
static void Main(string[] args)
{
Console.WriteLine("Please enter the Width and Height like \" 100, 100\", enter \"quit\" for quit");
bool isOut = false;
while(!isOut)
{
string rectInfo = Console.ReadLine();
if (!rectInfo.Equals("quit"))
{
try
{
string[] rectWidthAndHeight = rectInfo.Split(new char[] { ',' });
double width = double.Parse(rectWidthAndHeight[0]);
double height = double.Parse(rectWidthAndHeight[1]);
Rectangle myRect = new Rectangle(width, height);
Console.WriteLine("The area is " + myRect.Area.ToString());
Console.WriteLine("The circumference is " + myRect.Circumference.ToString());
}
catch { }
}
else
{
isOut = true;
}
}
}
程序如下:
……
class Rectangle
{
private double width;
private double height;
public double Width
{
get { return width; }
set { width = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public double Area
{
get
{return Width * Height;}
}
public double Circumference
{
get{return (Width + Height) * 2;}
}
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
}
//键盘处理范例:
static void Main(string[] args)
{
Console.WriteLine("Please enter the Width and Height like \" 100, 100\", enter \"quit\" for quit");
bool isOut = false;
while(!isOut)
{
string rectInfo = Console.ReadLine();
if (!rectInfo.Equals("quit"))
{
try
{
string[] rectWidthAndHeight = rectInfo.Split(new char[] { ',' });
double width = double.Parse(rectWidthAndHeight[0]);
double height = double.Parse(rectWidthAndHeight[1]);
Rectangle myRect = new Rectangle(width, height);
Console.WriteLine("The area is " + myRect.Area.ToString());
Console.WriteLine("The circumference is " + myRect.Circumference.ToString());
}
catch { }
}
else
{
isOut = true;
}
}
}
Rectangle myRect=new Rectangle();
myRect.Width=width;
myRect.Height=height;
……
class Rectangle
{
private double width;
private double height;
public double Width
{
get { return width; }
set { width = value; }
}
public double Height
{
get { return height; }
set { height = value; }
}
public double Area
{
get{return Width * Height;}
}
public double Circumference
{
get{return (Width + Height) * 2;}
}
public Rectangle(double width, double height)
{
Width = width;
Height = height;
}
static void Main(string[] args)
{
Console.WriteLine("Please enter the Width and Height like \" 100, 100\", enter \"quit\" for quit");
bool isOut = false;
while(!isOut)
{
string rectInfo = Console.ReadLine();
if (!rectInfo.Equals("quit"))
{
try
{
string[] rectWidthAndHeight = rectInfo.Split(new char[] { ',' });
double width = double.Parse(rectWidthAndHeight[0]);
double height = double.Parse(rectWidthAndHeight[1]);
Rectangle myRect = new Rectangle(width, height);
Console.WriteLine("The area is " + myRect.Area.ToString());
Console.WriteLine("The circumference is " + myRect.Circumference.ToString());
}
catch { }
}
else
{
isOut = true;
}
}
}
}
加上颜色的是说无法访问,因为是只读的;还有循环方式是不是应该改一下,while循环好像求不出来结果!
是不是应当改一下循环方式,不用while循环,改用if循环会不会更好??