-1
我正在嘗試打印多個信息條目,這些條目將作爲對象插入到數組中,並且我想使用可用於這些對象的方法並打印結果。這是我的代碼。我在我的最後兩句話得到一個錯誤如何在數組中打印對象?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace homework2
{
class Shapes
{
protected string ShapeName;
protected double ShapeWidth;
protected double ShapeHeight;
public Shapes(string ShapeName, double ShapeWidth, double ShapeHeight)
{
this.ShapeName = ShapeName;
this.ShapeWidth = ShapeWidth;
this.ShapeHeight = ShapeHeight;
}
}
class Rectangle : Shapes
{
public Rectangle(string ShapeName, double ShapeWidth, double ShapeHeight)
: base(ShapeName, ShapeWidth, ShapeHeight)
{
this.ShapeName = ShapeName;
this.ShapeWidth = ShapeWidth;
this.ShapeHeight = ShapeHeight;
}
public double GetArea()
{
if (ShapeName == "Circle")
{
ShapeHeight = 3.14;
double x = ShapeHeight * (ShapeWidth * ShapeWidth);
return x;
}
else
{
double Area = ShapeHeight * ShapeWidth;
return Area;
}
}
}
class Program
{
static void Main(string[] args)
{
Rectangle Rec = new Rectangle("Circle",5,2);
System.Console.WriteLine("This is the Rectangle Area :"+Rec.GetArea());
System.Console.WriteLine("Please Enter How Many Shapes You want To enter:");
String x = Console.ReadLine();
int y = int.Parse(x);
for (int i = 0; i <= y; i++)
{
System.Console.WriteLine("Enter Name for Shape No."+i+"Please");
String ShapeName = Console.ReadLine();
System.Console.WriteLine("Enter width for Shape No." + i + "Please");
String ShapeWidth = Console.ReadLine();
int sw = int.Parse(ShapeWidth);
System.Console.WriteLine("Enter height for Shape No." + i + "Please");
String ShapeHeight = Console.ReadLine();
int sh = int.Parse(ShapeHeight);
for(int j = 0; j < 4; j++)
{
Rectangle[,] z = new Rectangle[y,4];
Rectangle z[i,j] = new Rectangle(ShapeName, sw, sh);
}
}
}
}
}
什麼是錯誤? –
你有什麼錯誤?隨意刪除多餘的代碼以及... –
第一個錯誤是在最後一個z「局部變量名稱」z「已在範圍中定義」,並在數組中它說:「壞陣列減速和它說的相等「;預期「和新的」預期「,你是什麼意思的無關代碼請解釋 – user959349