C#面向对象习题创建Box盒子类

定义一个带构造方法的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;
        }

    }
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PSX_CFC^O^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值