我有兩個類。一個包含數據和行爲。另一個只是輸入和顯示數據。當我調用另一個類的方法來顯示結果時,它返回零。從一個類調用方法到另一個類?
任何幫助...感謝
數據和行爲:
class CalculateArea
{
private const double PI = 3.14;
private double cirArea;
private double recArea;
private double cilArea;
private double radius;
private double length, width;
private int height;
// Constructors
public CalculateArea(double radius1)
{
radius = radius1;
}
public double CalculateCircleArea()
{
cirArea = (PI * (radius * radius));
return cirArea;
}
public CalculateArea(int height1)
{
height = height1;
}
public CalculateArea(double lenght1 , double width1)
{
length = lenght1;
width = width1;
}
//public CalculateArea()
//{
// TODO: Complete member initialization
//}
// methods
//
public double CalculateRectangleArea()
{
recArea = (length * width);
return recArea;
}
//
public double CalculateCylinderArea()
{
cilArea = (PI * (radius * radius) * height);
return cilArea;
}
}
輸入和顯示:
class Program
{
static void Main(string[] args)
{
string number;
Console.WriteLine("Which figure do you want to see calculated select a number");
Console.WriteLine("Circle : 1");
Console.WriteLine("Rectangle : 2");
Console.WriteLine("Cylinder : 3");
number= Console.ReadLine();
int numb=Convert.ToInt32(number);
if (numb == 1)
{
Console.WriteLine("Enter the radius for circle");
string rad;
rad = Console.ReadLine();
int radiusX = Convert.ToInt32(rad);
CalculateArea newCal1 = new CalculateArea(radiusX);
Console.WriteLine("The result of calculation is {0}", newCal1.CalculateCircleArea());
}
else if(numb == 2)
{
Console.WriteLine("Enter the length ");
number = Console.ReadLine();
double lenght;
lenght = Convert.ToDouble(number);
Console.WriteLine("Enter the width ");
number = Console.ReadLine();
double width;
width = Convert.ToDouble(number);
CalculateArea newCal2=new CalculateArea(lenght , width);
Console.WriteLine("The result of calculation is {0}" , newCal2.CalculateRectangleArea());
}
else if (numb == 3)
{
Console.WriteLine("Enter the height ");
number = Console.ReadLine();
int height;
height = Convert.ToInt32(number);
Console.WriteLine("Enter the radius");
number = Console.ReadLine();
int radius;
radius = Convert.ToInt32(number);
CalculateArea newCal3 = new CalculateArea(height, radius);
Console.WriteLine("The reslut of calculation is {0}", newCal3.CalculateCylinderArea());
}
else
{
Console.WriteLine("There is no any calculation number check the information and try again");
}
Console.ReadLine();
}
你有什麼困難? – eurotrash
我想返回計算結果,但每次返回0 – jawed
計算結果 – jawed