如何调用方法来获取三角形的区域

时间:2014-02-04 12:38:39

标签: c# methods

这是我的代码,但我不知道用哪个关键字将我的主代码链接到方法。有什么帮助吗?

try
{
    double numA = Convert.ToInt32(tbx1.Text);
    double numB = Convert.ToInt32(tbx2.Text);
    double numC = Convert.ToInt32(tbx3.Text);

    double area;

    area = (numA + numB + numC) / 2;

    tblk1.Text = area.ToString();
}
catch (FormatException fE)
{
    MessageBox.Show("Input must be in text format");
}
catch (Exception eX)
{
    MessageBox.Show("Number is negative!");
}

public void area(double area)
{
    area = (numA + numB + numC) / 2;
    return area;
}

2 个答案:

答案 0 :(得分:2)

您需要为double功能返回area(),而不是void。您还需要一个不同的参数列表:

public double area(double height, double length, double width)
{
    area = (height * length * width) / 2;
    return area;
}

编辑,这应该是更合适的命名音量,但在这里你可以选择:

public double area(double length, double width)
{
    var x = (length * width);
    return x;
}

public double volume(double height, double length, double width)
{
    var x = (height * length * width);
    return x;
}

答案 1 :(得分:1)

试试这个:

try
{
    double numA = Convert.ToInt32(tbx1.Text); 
    double numB = Convert.ToInt32(tbx2.Text);
    double numC = Convert.ToInt32(tbx3.Text);

    tblk1.Text = area(numA, numB, numC).ToString();
}
catch (FormatException fE)
{
    MessageBox.Show("Input must be in text format");
}
catch (Exception eX)
{
    MessageBox.Show("Number is negative!");
}

public double area(int numA, int numB, int numC)
{
    area = (numA + numB + numC) / 2;
    return area;
}