C#练习题答案: 建筑模块【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

建筑模块【难度:1级】:

答案1:

class Block
{
  public int Width { get; private set; }
  public int Length { get; private set; }
  public int Height { get; private set; }

  public Block(int[] properties) {
    this.Width = properties[0];    
    this.Length = properties[1];
    this.Height = properties[2];
  }

  public int GetWidth() { return Width; }

  public int GetLength() { return Length; }

  public int GetHeight() { return Height; }

  public int GetVolume() { return GetWidth() * GetHeight() * GetLength(); }

  public int GetSurfaceArea() {
    return   (2 * GetLength() * GetWidth())
           + (2 * GetLength() * GetHeight())
           + (2 * GetWidth() * GetHeight());
  }
}

答案2:

using System;

class Block
{
   private readonly int _width;
   private readonly int _length;
   private readonly int _height;
   
   public Block(int[] p)
   {
     if(p.Length != 3 || p[0] <= 0 || p[1] <= 0 || p[2] <= 0)
     {
       throw new ArgumentException();
     }
     
     this._width = p[0];
     this._length = p[1];
     this._height = p[2];
   }
   
   public int GetWidth()
   {
     return _width;
   }
   
   public int GetLength()
   {
     return _length;
   }
   
   public int GetHeight()
   {
     return _height;
   }
   
   public int GetVolume()
   {
     return _width*_length*_height;
   }
   
   public int GetSurfaceArea()
   {
     return 2*(_width*_length + _length*_height + _width*_height);
   }
}

答案3:

class Block
{
  private int width, length, height;

  public Block(int[] data)
  {
    width = data[0];
    length = data[1];
    height = data[2];
  }
  
  public int GetWidth()
  {
    return width;
  }
  
  public int GetLength()
  {
    return length;
  }
  
  public int GetHeight()
  {
    return height;
  }
  
  public int GetVolume()
  {
    return width *length* height;
  }
  
  public int GetSurfaceArea()
  {
    return 2*(width*length + length*height + height*width);
  }
}

答案4:

class Block
{
  int[] dimension;
  
  public Block(int[] dim) {
    dimension = dim;
  }
  
  public int GetWidth() {
    return dimension[0];
  }
  
  public int GetLength() {
    return dimension[1];
  }
  
  public int GetHeight() {
    return dimension[2];
  }
  
  public int GetVolume() {
    return dimension[0] * dimension[1] * dimension[2];
  }
  
  public int GetSurfaceArea() {
    return 2 * (dimension[0] * dimension[1] + dimension[1] * dimension[2] + dimension[2] * dimension[0]);
  }
}

答案5:

class Block
{
  int width;
  int length;
  int height;
  public Block(int[] dimensions)
  {
    this.width = dimensions[0];
    this.length = dimensions[1];
    this.height = dimensions[2];
  }
  public int GetWidth(){return width;}
  public int GetLength(){return length;}
  public int GetHeight(){return height;}
  public int GetVolume(){return width *length* height;}
  public int GetSurfaceArea(){return 2 * (width*length+width*height+length*height);}
}

答案6:

using System;
class Block
{
  
  private int width;
  
  private int length;
  
  private int height;

  // Gooc Luck!
  public Block(int[] args){
    if(args.Length != 3)
    throw new Exception("invalid arguments");
    SetWidth(args[0]);
    SetLength(args[1]);
    SetHeight(args[2]);
  }
  
  private void SetWidth(int width){
    this.width = width;
  }
  
  private void SetLength(int length){
    this.length = length;
  }
  
  private void SetHeight(int height){
    this.height = height;
  }
  
  
  public int GetWidth(){
    return this.width;
  }
  
  public int GetLength(){
    return this.length;
  }
  
  public int GetHeight(){
    return this.height;
  }
  
  public int GetVolume(){
    return this.width * this.height * this.length;
  }
  
  public int GetSurfaceArea(){
    return 2 * (this.width * this.height + this.width * this.length + this.height * this.length);
  }
}

答案7:

class Block {
  private int w, l, h;
  public Block(int[] dim) {
    w = dim[0]; l = dim[1]; h = dim[2];
  }
  public int GetWidth() {return w;}
  public int GetLength() {return l;}
  public int GetHeight() {return h;}
  public int GetVolume() {return w *l* h;}
  public int GetSurfaceArea() {
    return 2 *w* l + 2 *w* h + 2 *l* h;
  }
}

答案8:

class Block
{
  private int _width { get; set; }
  private int _length { get; set; }
  private int _height { get; set; }
  
  public Block(int[] arr)
  {
    _width = arr[0];
    _length = arr[1];
    _height = arr[2];
  }
  
  public int GetWidth() => _width;
  public int GetLength() => _length;
  public int GetHeight() => _height;
  public int GetVolume()
  {
    return _width * _length * _height;
  }
  public int GetSurfaceArea()
  {
    return 2 * _length * _width + 2 * _length * _height + 2 * _width * _height;
  }
}

答案9:

class Block
{
  public int[]a{get;set;}
  public Block(int[]b){a=b;}
  public int GetWidth(){return a[0];}
  public int GetLength(){return a[1];}
  public int GetHeight(){return a[2];}
  public int GetVolume(){return a[0]*a[1]*a[2];}
  public int GetSurfaceArea(){return (a[0]*a[1]+a[0]*a[2]+a[1]*a[2])*2;}
}

答案10:

class Block
{
  // Gooc Luck!
  private int width;
  private int height;
  private int length;
  
  public Block(int[] values)
  {
     width = values[0];
     height = values[1];
     length= values[2];
  }
  
  public int GetWidth()
  {
    return width;
  }
  
  public int GetLength()
  {
    return height;
  }
  
  public int GetHeight()
  {
    return length;
  }
  
  public int GetVolume()
  {
    return length *height* width;
  }
  
   public int GetSurfaceArea()
  {
    return 2 * (length * height + width * height + width * length);
  }
  
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值