【发布时间】:2016-11-18 06:40:39
【问题描述】:
我设法让程序接受值,但我不太确定如何打印我的结果,当我尝试像这样打印它时,这是错误的,它只是给了我随机文本。我想应该是 sh.display 之类的,但我真的迷路了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
abstract class shape
{
void getarea()
{ Console.WriteLine("Area is"); }
void display()
{
Console.WriteLine("This is shape");
}
void getcirc()
{
Console.WriteLine("Circumference is");
}
}
class circle : shape
{
public circle(double r)
{this.r = r;}
double r;
void getarea()
{
Console.WriteLine("Circle area =" + (Math.PI * Math.Pow(r, 2)));
}
void getcirc()
{
Console.WriteLine("Circle circumference = " + (2 * Math.PI * r));
}
void display()
{
Console.WriteLine("This is circle");
}
}
class rect : shape
{
public rect(double x, double y)
{this.x=x;
this.y=y;
}
double x,y;
void getarea()
{
Console.WriteLine("Rectangle area =" + (x * y));
}
void getcirc()
{
Console.WriteLine("Circle circumference = " + ((2 * x) + (2 * y)));
}
void display()
{
Console.WriteLine("This is Rectangle");
}
class square : shape
{
public square(double z)
{this.z=z;
}
double z;
void getarea()
{
Console.WriteLine("Square area =" + (z*z));
}
void getcirc()
{
Console.WriteLine("Square perimeter = " + (4 * z));
}
void display()
{
Console.WriteLine("This is square");
}
class Program
{
static void Main(string[] args)
{
shape[] sh = new shape[15];
Random rndm= new Random();
int i;
int shapenum;
for (i = 0; i < 15; i++)
{ shapenum = rndm.Next(1,4);
switch (shapenum)
{
case 1:
Console.WriteLine("Enter radius");
sh[i] = new circle(int.Parse(Console.ReadLine()));
break;
case 2:
Console.WriteLine("Enter x and y lengths");
double x = double.Parse(Console.ReadLine());
double y = double.Parse(Console.ReadLine());
sh[i] = new rect(x,y);
break;
case 3:
Console.WriteLine("Enter side length");
sh[i] = new square(int.Parse(Console.ReadLine()));
break;
}
}
for (i = 0; i < 15; i++)
{
Console.Write(sh[i] + " ");
}
}
}
}
}
}
【问题讨论】:
-
定义“给我随机文本”...
-
ConsoleApplication1.rect ConsoleAppication1.rect CosnoleApplication1.circle ConsoleApplication1.Circle ConsoleApplication1.rect+square
-
这不是随机的。这正是它应该打印的内容。尝试将
Console.Write(sh[i] + " ");替换为sh[i].display();。 -
请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。
-
哦,我应该输入 15 个随机元素(三种形状之一),然后使用循环打印每个元素的数据(面积、周长/周长和类型),使用三种方法(显示, getarea 和 getcirc) 在每个类中。它还说 shape.display() 由于其保护级别而无法访问
标签: c# arrays object printing abstract