2016-11-18 130 views
0

我設法讓程序接受值,但我不太清楚如何打印我的結果,當我試圖打印它,這是錯誤的,它只是給了我隨機文本。我想這應該是這樣的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] + " "); 
        } 


       } 
      } 
     } 
    } 
} 
+1

定義 「給我隨機文本」 ... –

+0

ConsoleApplication1.rect ConsoleAppication1。 rect CosnoleApplication1.circle ConsoleApplication1.Circle ConsoleApplication1.rect + square – Decla

+2

這不是隨機的。這正是它應該打印的。嘗試用'sh [i] .display();'替換'Console.Write(sh [i] +「」);''。 –

回答

0

你的類沒有覆蓋.ToString(),所以當你調用Console.Write(sh[i] + " ")它只是打印對象的類型名稱。 (不是「隨機文本」)

你需要做兩件事情之一:

  1. 定義你的班.ToString()倍率(最簡單的,推薦);
  2. shape.display()一個virtual方法(virtual void display()),並在所有的子類使用override void display(),然後調用sh[i].display()代替Console.Write(sh[i] + " ");

任一人都可以解決您的問題。

當然,如果我們的目標是使從shape派生的所有類getarea()getcirc(),那麼你應該將它們標記上shapevirtual,然後標記他們的子類override

更重要的是,與interfaceIShape)取代shape,然後讓所有的子類簡單實現接口。

+0

'sh [i] .display()'不會工作。 Shape是一個抽象類 –

+0

@MohitShrivastava已更新。 –

+0

謝謝我的代碼現在可以工作。 :) – Decla

0

使用Console.Write(sh[i] + " ")將輸出對象引用到控制檯,因爲shape是一個引用類型,或簡單的話說,一個對象,而不是一個基元/值類型。這是您正在討論的「隨機文本」的輸出。

現在出現了你想爲你的目的顯示的問題。如果你想只顯示「這是一個正方形」或「這是一個圈」,你將不得不調用使用 Console.Write(sh[i].display() + " ")

同樣的顯示功能,您可能需要調用sh[i].getcirc()sh[i].getarea()打印的圓周和區。

+0

使用循環輸入15個隨機元素(圓形或矩形或方形)後,我應該使用另一個循環來使用三種方法打印每個元素的數據(面積,周長/周長和類型),但它不讓我使用sh [i] .getcirc因爲形狀是一個抽象類, – Decla

+0

您應該像'class Circle'一樣在class rect之外聲明'class Program'和'class Square'。目前,它們嵌套在一起,因此你的代碼不起作用。 – tedghosh

0

我重構了你的代碼並修復了這個問題。關鍵時刻:

  • 對方法使用抽象/覆蓋詞。
  • 重寫的ToString()爲基礎類
  • 按照C#的命名約定

下面是代碼:

using System; 

namespace ConsoleApplication1 
{ 
    abstract class Shape 
    { 
    public abstract string GetArea(); 
    public abstract string Display(); 
    public abstract string GetPerimeter(); 
    public override string ToString() 
    { 
     return Display() + ": " + GetArea() + ", " + GetPerimeter(); 
    } 
    } 

    class Circle : Shape 
    { 
    public Circle(double r) 
    { 
     this.r = r; 
    } 

    double r; 

    public override string GetArea() 
    { 
     return "Circle area =" + Math.PI*Math.Pow(r, 2); 
    } 

    public override string GetPerimeter() 
    { 
     return "Circle circumference = " + 2*Math.PI*r; 
    } 

    public override string Display() 
    { 
     return "This is Circle"; 
    } 
    } 

    class Rect : Shape 
    { 
    public Rect(double x, double y) 
    { 
     this.x = x; 
     this.y = y; 
    } 

    double x, y; 

    public override string GetArea() 
    { 
     return "Rectangle area =" + x*y; 
    } 

    public override string GetPerimeter() 
    { 
     return "Circle circumference = " + (2*x + 2*y); 
    } 

    public override string Display() 
    { 
     return "This is Rectangle"; 
    } 
    } 

    class Square : Shape 
    { 
    public Square(double z) 
    { 
     this.z = z; 
    } 

    double z; 

    public override string GetArea() 
    { 
     return "Square area =" + z*z; 
    } 

    public override string GetPerimeter() 
    { 
     return "Square perimeter = " + 4*z; 
    } 

    public override string Display() 
    { 
     return "This is Square"; 
    } 

    } 

    class Program 
    { 
    static void Main(string[] args) 
    { 
     var sh = new Shape[15]; 
     var rndm = new Random(); 

     for (var i = 0; i < 15; i++) 
     { 
     switch (rndm.Next(1, 4)) 
     { 
      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; 
     } 
     } 

     foreach (var shape in sh) 
     Console.WriteLine(shape); 
     Console.ReadLine(); 
    } 
    } 
}