C# 使用抽象类使用用户输入创建不同的类
我正在尝试创建一个程序,其中用户选择1、2、3来选择矩形、三角形或正方形;或零退出。然后用户给出形状的宽度和高度,然后输出形状名称、宽度和高度,并计算面积 在我的公共矩形(int-width,int-height)和公共正方形(int-height)以及公共三角形(int-width,int-height)类中,它们都给出了相同的错误……”没有给出与GeometricFigure.GeometricFigure的必需形式参数“width”相对应的参数。GeometricFigure(int,int)这到底意味着什么以及如何修复它 我只是想让我的逻辑正确任何帮助都是感激的 使用制度C# 使用抽象类使用用户输入创建不同的类,c#,C#,我正在尝试创建一个程序,其中用户选择1、2、3来选择矩形、三角形或正方形;或零退出。然后用户给出形状的宽度和高度,然后输出形状名称、宽度和高度,并计算面积 在我的公共矩形(int-width,int-height)和公共正方形(int-height)以及公共三角形(int-width,int-height)类中,它们都给出了相同的错误……”没有给出与GeometricFigure.GeometricFigure的必需形式参数“width”相对应的参数。GeometricFigure(int,in
class Program
{
static void Main(string[] args)
{
Rectangle r;
Square s;
Triangle t;
int number;
string value = null;
do
{
Console.WriteLine("Choose a shape from the following options ");
Console.WriteLine("[1] Rectangle");
Console.WriteLine("[2] Square");
Console.WriteLine("[3] Triangle");
Console.WriteLine("Enter the option number (or zero to terminate this program): ");
number = Int32.Parse(value);
Console.Write(number);
}
while (number != 0);
if (number == 0)
{
Console.WriteLine();
Console.WriteLine("Please press <enter> to terminate this program");
Console.ReadLine();
}
else if (number == 1)
{
Console.Write("Please press enter the width and then hit enter: ");
int width = Int32.Parse(Console.ReadLine());
Console.Write("Please press enter the height and then hit enter: ");
int height = Int32.Parse(Console.ReadLine());
r = new Rectangle(width, height);
}
else if (number == 2)
{
Console.Write("Please press enter the height and then hit enter: ");
int height = Int32.Parse(Console.ReadLine());
s = new Square(height);
}
else if (number == 3)
{
Console.Write("Please press enter the width and then hit enter: ");
int width = Int32.Parse(Console.ReadLine());
Console.Write("Please press enter the height and then hit enter: ");
int height = Int32.Parse(Console.ReadLine());
t = new Triangle(width, height);
}
}
}
public abstract class GeometricFigure
{
private readonly int width;
private readonly int height;
private readonly double area;
public GeometricFigure(int width, int height)
{
}
public abstract double computeArea
{
get;
}
public int Width
{
get { return width; }
}
public int Height
{
get { return height; }
}
}
public class Rectangle : GeometricFigure
{
private int width;
private int height;
public Rectangle(int width, int height)
{
}
public override double computeArea
{
get { return width * height; }
}
}
public class Square : Rectangle
{
private int side;
public Square(int height)
{
side = height;
}
public override double computeArea
{
get
{
// Given the side, return the area of a square:
return side * side;
}
}
}
public class Triangle : GeometricFigure
{
public Triangle(int width, int height)
{
}
public override double computeArea
{
get { return (Width * Height) / 2; }
}
}
类程序
{
静态void Main(字符串[]参数)
{
矩形r;
正方形s;
三角形t;
整数;
字符串值=null;
做
{
WriteLine(“从以下选项中选择一个形状”);
Console.WriteLine(“[1]矩形”);
控制台写线(“[2]方形”);
控制台。写线(“[3]三角形”);
Console.WriteLine(“输入选项号(或零终止此程序):”;
number=Int32.Parse(值);
控制台。写入(数字);
}
while(number!=0);
如果(数字==0)
{
Console.WriteLine();
Console.WriteLine(“请按以终止此程序”);
Console.ReadLine();
}
else if(数字==1)
{
控制台。写入(“请按输入宽度,然后按enter:”;
int width=Int32.Parse(Console.ReadLine());
控制台。写入(“请按输入高度,然后按enter:”;
int height=Int32.Parse(Console.ReadLine());
r=新矩形(宽度、高度);
}
else if(数字=2)
{
控制台。写入(“请按输入高度,然后按enter:”;
int height=Int32.Parse(Console.ReadLine());
s=新广场(高度);
}
else if(数字=3)
{
控制台。写入(“请按输入宽度,然后按enter:”;
int width=Int32.Parse(Console.ReadLine());
控制台。写入(“请按输入高度,然后按enter:”;
int height=Int32.Parse(Console.ReadLine());
t=新三角形(宽度、高度);
}
}
}
公共抽象类几何图形
{
私有只读整型宽度;
私有只读int高度;
私人只读双区;
公共几何图形(整数宽度、整数高度)
{
}
公共抽象双计算区
{
得到;
}
公共整数宽度
{
获取{返回宽度;}
}
公共内部高度
{
获取{返回高度;}
}
}
公共类矩形:几何图形
{
私有整数宽度;
私人内部高度;
公共矩形(整数宽度、整数高度)
{
}
公共覆盖双计算机区域
{
获取{返回宽度*高度;}
}
}
公共类正方形:矩形
{
私有内部侧;
公众广场(内部高度)
{
侧面=高度;
}
公共覆盖双计算机区域
{
得到
{
//给定边,返回正方形的面积:
返回侧*侧;
}
}
}
公共类三角形:几何图形
{
公共三角形(整数宽度、整数高度)
{
}
公共覆盖双计算机区域
{
获取{return(Width*Height)/2;}
}
}
首先,您从何处获取用户输入?您声明了值,但除了null之外,您从未为其分配任何内容
为了回答您的问题,您使用四个参数(数字、高度、宽度和面积)声明矩形构造函数,但是当你调用构造函数时,你没有给它任何值。我不明白为什么数字和面积需要成为它的构造函数的参数,它只关心它的高度、宽度和面积,所以给它数字没有意义,给它面积没有意义,因为它应该计算,而不是告诉它
为了解决您的问题,我将构造函数的参数更改为仅int-width,int-height
,然后将宽度和高度传递给构造函数(矩形(someValue,someOtherValue);
)
您还需要将此修复应用于正方形和三角形
第三,一个小的语法错误,在向基构造函数传递参数时,不需要重述类型。只需声明要传递的变量名或值即可
如果这有点像信息转储,我很抱歉,但我希望它能有所帮助。我建议你先注释掉正方形和三角形,这样你就可以专注于矩形(或者你喜欢的任何形状) 您的几何图形需要一个名为“area”的抽象属性,请参见 您的
矩形
构造函数不应使用类型声明调用基构造函数,并且参数名称必须匹配。要向您说明我的意思,请执行以下操作:
base(int number, int _width, int _height, double area)
应该是
base(number, width, height, area)
您需要向构造函数传递一些值,如下所示:
int number = 1; // The shape you want
int width = 10;
int height = 20;
double area = width * height;
r = new Rectangle(number, width, height, area)
您还应该声明矩形,而不必像@jonesopolis建议的那样实例化
e、 g
希望有帮助
编辑:
下面是从控制台读取一些数字并将其传递给矩形构造函数的示例:
else if (number == 1)
{
Console.Write("Please press enter the width and then hit enter: ");
int width = Int32.Parse(Console.ReadLine());
Console.Write("Please press enter the height and then hit enter: ");
int height = Int32.Parse(Console.ReadLine());
double area = width * height;
r = new Rectangle(number, width, height, area);
}
你可以在不实例化的情况下声明一个对象。从逻辑上讲,你不能创建一个没有高度和宽度的矩形;从语法上讲,你的构造函数需要这些参数。在你知道如何实例化之前,不要实例化你的对象。但是这段代码太乱了,无法给出一个好的答案。是的,我对用m做任何事情都感到迷茫这个问题我已经上了不止一节课了
else if (number == 1)
{
Console.Write("Please press enter the width and then hit enter: ");
int width = Int32.Parse(Console.ReadLine());
Console.Write("Please press enter the height and then hit enter: ");
int height = Int32.Parse(Console.ReadLine());
double area = width * height;
r = new Rectangle(number, width, height, area);
}