所以我在C#中編寫了一個二次公式程序,我如何取二次公式程序並對其進行修改,以便程序正確顯示解決方案的數量。如何顯示二次公式的解決方案
如果有兩個解決方案,
(x - x1)(x - x2) = 0
如果只有一個解決方案,
(x - x0)^2 = 0
如果沒有解決方案,
無解。
這是一個程序,如果有人能爲我展示這個解決方案對我來說將是美好的,我真的被困在如何去做。
using System;
namespace quadraticequation
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number for a"); //ask the user for information
double a = double.Parse(Console.ReadLine()); //Gets a from the user
Console.WriteLine("Enter a number for b"); //asks the user for information
double b = double.Parse(Console.ReadLine()); //Gets b from the user
Console.WriteLine("Enter a number for c"); //asks the user for information
double c = double.Parse(Console.ReadLine()); //Gets c from the user
//double.Parse --> is used to convert a number or string to a double.
//Console.ReadLine() --> is used to take the input from the user.
//We call a function here
Quadratic(a, b, c);
}
//We need to create a new function
public static void Quadratic(double a, double b, double c)
{
double deltaRoot = Math.Sqrt(b * b - 4 * a * c); //Math.Sqrt takes the square root of the number
if (deltaRoot >= 0) // we use an if statement here to handle information
{
double x1 = (-b + deltaRoot)/2 * a; //We write the information for x1 here
double x2 = (-b - deltaRoot)/2 * a; //We write the information for x2 here
Console.WriteLine("x1 = " + x1 + " x2 = " + x2); //we use this to write the roots
}
else // we use an else statement so that we dont return an error when there are no roots
{
Console.WriteLine("There are no roots");
}
}
}
}
我是正確的假設,只希望*實時*解決方案,而不是複雜的? –