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

编程入门 行业动态 更新时间:2024-10-25 20:29:22

C#面向对象<a href=https://www.elefans.com/category/jswz/34/1769768.html style=习题创建Box盒子类"/>

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;}}
}

更多推荐

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

本文发布于:2024-02-12 00:43:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:习题   面向对象   盒子   Box

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!