定义一个带构造方法的Box盒子类。要求可以设置形状,可计算体积和表面积
初学者,作笔记发,代码写的一般。望各位大佬提出批评和建议。
using System.Reflection.Metadata;
namespace _3.2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("长方体输入0,圆柱体输入1");
int shp = Convert.ToInt32(Console.ReadLine());
if (shp == 0)
{
Console.WriteLine("输入长:");
int len = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入宽:");
int wid = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入高:");
int hei = Convert.ToInt32(Console.ReadLine());
Box box = new Box(len, wid, hei);
int area = box.areaCuboid();
int V = box.volumeCuboid();
Console.WriteLine("长方体面积是{0}体积是{1}", area, V);
}
else if (shp == 1)
{
Console.WriteLine("输入高:");
int hei = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入半径:");
int r = Convert.ToInt32(Console.ReadLine());
Box box = new Box(r,hei);
int area = box.areaCylinder();
int V = box.volumeCylinder();
Console.WriteLine("圆柱体面积是{0}体积是{1}", area, V);
}
Console.ReadLine();
}
}
public class Box
{
public int length, width, height, R;
public Box(int len, int wid, int hei) //构造函数1长方体盒子
{
length = len;
width = wid;
height = hei;
}
public Box(int r,int hei) //构造函数2圆柱体盒子
{
R = r;
height = hei;
}
public int areaCuboid() //方法:计算长方体面积
{
int area = 2*(length*width + width*height + length*height);
return area;
}
public int volumeCuboid() //方法:计算长方体体积
{
int volume = length*width*height;
return volume;
}
public int areaCylinder() //方法:计算圆柱体表面积
{
int area = 2*(int)Math.PI*R*R + 2*(int)Math.PI*R*height;
return area;
}
public int volumeCylinder() //方法:计算圆柱体体积
{
int volume = (int)Math.PI * R * R * height;
return volume;
}
}
}