根据题目要求:CCylinder和CCone各自包含一个CCircle类型的成员c,以及两个double类型的变量h和volume,分别表示圆柱体和圆锥体的高和体积。
不知道怎么理解,觉得除了高度,其他成员用不上,我在程序中将其作为字段来定义的,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 计算圆柱体和圆锥体的体积
{
public class CCircle
{
private double r;
public double R
{
get { return r; }
set { r = value; }
}
}
public class CCylinder
{
CCircle c = new CCircle();
private double h;
public double H
{
get { return h; }
set { h = value; }
}
private double volume;
public double Volume
{
get { return volume; }
set { volume = value; }
}
public double CalculateVolume(double r,double h)
{
return Math.PI * r * r * h;
}
}
public class CCone
{
CCircle c = new CCircle();
private double h;
public double H
{
get { return h; }
set { h = value; }
}
private double volume;
public double Volume
{
get { return volume; }
set { volume = value; }
}
public double CalculateVolume(double r,double h)
{
return Math.PI * r * r * h/3;
}
}
class Program
{
static void Main(string[] args)
{
CCylinder cy1=new CCylinder ();
CCone cone1=new CCone ();
Console.WriteLine("请输入圆柱体底面半径");
double myr = double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆柱体高度");
double myh = double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆锥体高度");
double myhccone = double.Parse(Console.ReadLine());
Console.WriteLine("圆柱体体积是{0}", cy1.CalculateVolume (myr,myh) );
Console.WriteLine("圆锥体体积是{0}", cone1.CalculateVolume(myr, myh));
}
}
}
Console.WriteLine("请输入圆柱体底面半径");
double a = double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆柱体高度");
double b= double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆锥体高度");
double c = double.Parse(Console.ReadLine());
double aa;
aa = 3.14*(a *a)* b;
Console.WriteLine("圆柱体体积是{0}", aa);
double bb;
bb = (c/3) * 3.1415926;
Console.WriteLine("圆锥体体积是{0}", bb);
Console.ReadKey();
基本上和你要的效果差不多,但是我只看了你的问题,没有看你的补充。所以。。。
这是我刚写的代码
希望可以帮到你,
Console.WriteLine("请输入圆柱体底面半径");
double a = double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆柱体高度");
double b= double.Parse(Console.ReadLine());
Console.WriteLine("请输入圆锥体高度");
double c = double.Parse(Console.ReadLine());
double aa;
aa = 3.14*(a *a)* b;
Console.WriteLine("圆柱体体积是{0}", aa);
double bb;
bb = (c/3) * 3.1415926;
Console.WriteLine("圆锥体体积是{0}", bb);
Console.ReadKey();
我是初学者,不好的地方,请指点!