私は、1,2,3を選択して四角形、三角形、または四角形を選択するプログラムを作成しようとしています。終了するには0を入力します。次に、ユーザーが形状の幅と高さを指定し、形状名、幅、高さを出力し、面積を計算します。私のパブリックRectangle(int幅、int高さ)と私のパブリックSquare(int高さ)とパブリックTriangle(int幅、int高さ)クラスのすべての同じエラーを与える... "引数はありません私はちょうど私のロジックを取得しようとしている抽象クラスを使用すると、ユーザー入力で異なるクラスを作成する
?まさにその平均値とどのようにそれを修正するには何をするかGeometricFigure.GeometricFigureの必要な仮パラメータ「幅」(int型、int型)に対応して右の任意のヘルプは歓迎です!
をシステムを使用して;
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; }
}
}
することができます*宣言*オブジェクト* *それをインスタンス化せずに。論理的には、高さと幅を持たない矩形を作成することはできません。構文的には、コンストラクタのパラメータが必要です。方法を知るまでオブジェクトをインスタンス化しないでください。しかし、このコードは良い答えのためにあまりにも混乱です。 – Jonesopolis
ええ、私は今、この問題に4日間服従してきた複数のクラスで何かをすることになります。 – mvanderk10