我的平方根 C# 程序仅在判别式为 0 或小于 0 时有效,但在大于 0 时无效

My c# program for square roots only works when the discriminant is 0 or less than 0, but not when it's greater than 0

private void btnCompute_Click(object sender, EventArgs e)
{
    double coefficientA;
    double coefficientB;
    double coefficientC;
    double root1;
    double root2;
    double discriminant;

    coefficientA = double.Parse(txtCoeA.Text);
    coefficientB = double.Parse(txtCoeB.Text);
    coefficientC = double.Parse(txtCoeC.Text);

    discriminant = 
            (coefficientB * coefficientB) - 
            (4 * coefficientA * coefficientC);

    txtOutput.Text = "Discriminant = " + discriminant.ToString("N2");

    //-b/2a

    root1 = (-coefficientB + discriminant) / (2 * coefficientA);
    root2 = (-coefficientB - discriminant) / (2 * coefficientA);

    if (discriminant < 0)
    {
            txtOutput.Text += "\r\nEquation has no real roots!";
    }
    else if (discriminant == 0)
    {
        txtOutput.Text += 
            "\r\nEquation has one root: " + root2.ToString("N2");
    }        
    else if (discriminant > 0)
    {
        txtOutput.Text = "Equation has two real roots: " +
            "\r\nroot1: " + 
            (-coefficientB + Math.Sqrt(discriminant) / 
            (2 * coefficientA)) +
            "\r\nroot2: " + (coefficientB - Math.Sqrt(discriminant) / 
            (2 * coefficientA)); 
    }
}

试试这个:

    txtOutput.Text = "Equation has two real roots: " +
        "\r\nroot1: " + 
        (-coefficientB + Math.Sqrt(discriminant)) / (2 * coefficientA) +
        "\r\nroot2: " +
        (-coefficientB - Math.Sqrt(discriminant)) / (2 * coefficientA);

你把公式括起来不正确。