我設法讓程序接受值,但我不太清楚如何打印我的結果,當我試圖打印它,這是錯誤的,它只是給了我隨機文本。我想這應該是這樣的sh.display或東西,但我真的失去了這裏基本c#陣列打印
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 – Decla
這不是隨機的。這正是它應該打印的。嘗試用'sh [i] .display();'替換'Console.Write(sh [i] +「」);''。 –