C# get root方法中out参数问题
C# out parameter problem in the get root method
下面的代码从控制台获取 3 个输入,然后解析数字,然后将其发送到 getRealRoots
方法。找出它是否有 2、1 或没有根。 getrealroots 中的输出参数显示以下错误:
The out parameter 'r1' must be assigned to before control leaves the
current method
The out parameter 'r2' must be assigned to before control leaves the current method
using System;
namespace Quadratic
{
public class Program
{
static public void Main(string[] args)
{
Console.WriteLine("Enter three numbers, (A,B,C)");
Double? a = GetDouble();
Double? b = GetDouble();
Double? c = GetDouble();
getRealRoots(a, b, c,out r1,out r2);
//throw new NotImplementedException("implement main");
}
static public int getRealRoots(double A, double B, double C, out double? r1, out double? r2)
{
double discriminant = B * B - 4 * A * C;
if (discriminant > 0)
{
r1 = (-B + Math.Sqrt(discriminant)) / (2 * A);
r2 = (-B - Math.Sqrt(discriminant)) / (2 * A);
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has two real roots:" + r1 + " " + r2);
}
else if (discriminant == 0)
{
r1 = -B / (2 * A);
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has one real root:" + r1);
}
else
{
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has no real roots:");
}
}
//throw new NotImplementedException("write a method that uses out variables to return the real discriminants of a quadratic");
}
}
首先,您有 return 类型的 int,但没有 return int。
其次,错误消息说无论您的方法采用何种执行路径,您都必须为输出参数分配一些值。您可以通过在方法开头为它们分配一些 "default" 值来解决此问题。也许像这样?:
r1 = default (double);
r2 = null;
希望能帮到你
作为 per the documentation 在 out 参数修饰符上:
Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.
对于您提供的代码,在方法 getRealRoots
中,您是:
- 设置
r1
和 r2
的输出值,其中 discriminant > 0
- 设置
r1
的输出值,其中 discriminant == 0
,而不是 r2
的值
- 未设置
r1
,或r2
满足上述条件none。
由于被调用的方法需要赋值,你必须设置r1
和r2
在每个执行路径中。
由于您已将值定义为可空类型,因此您可以使用一些默认值开始您的方法来解决您的问题:
static public int getRealRoots(double A, double B, double C, out double? r1, out double? r2)
{
r1 = null;
r2 = null;
// ... your method code
}
然后在您设置的特定 IF 条件下覆盖默认值。
下面的代码从控制台获取 3 个输入,然后解析数字,然后将其发送到 getRealRoots
方法。找出它是否有 2、1 或没有根。 getrealroots 中的输出参数显示以下错误:
The out parameter 'r1' must be assigned to before control leaves the current method
The out parameter 'r2' must be assigned to before control leaves the current method
using System;
namespace Quadratic
{
public class Program
{
static public void Main(string[] args)
{
Console.WriteLine("Enter three numbers, (A,B,C)");
Double? a = GetDouble();
Double? b = GetDouble();
Double? c = GetDouble();
getRealRoots(a, b, c,out r1,out r2);
//throw new NotImplementedException("implement main");
}
static public int getRealRoots(double A, double B, double C, out double? r1, out double? r2)
{
double discriminant = B * B - 4 * A * C;
if (discriminant > 0)
{
r1 = (-B + Math.Sqrt(discriminant)) / (2 * A);
r2 = (-B - Math.Sqrt(discriminant)) / (2 * A);
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has two real roots:" + r1 + " " + r2);
}
else if (discriminant == 0)
{
r1 = -B / (2 * A);
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has one real root:" + r1);
}
else
{
Console.WriteLine("The equation " + GetQuadraticString(A, B, C) + " has no real roots:");
}
}
//throw new NotImplementedException("write a method that uses out variables to return the real discriminants of a quadratic");
}
}
首先,您有 return 类型的 int,但没有 return int。
其次,错误消息说无论您的方法采用何种执行路径,您都必须为输出参数分配一些值。您可以通过在方法开头为它们分配一些 "default" 值来解决此问题。也许像这样?:
r1 = default (double);
r2 = null;
希望能帮到你
作为 per the documentation 在 out 参数修饰符上:
Variables passed as out arguments do not have to be initialized before being passed in a method call. However, the called method is required to assign a value before the method returns.
对于您提供的代码,在方法 getRealRoots
中,您是:
- 设置
r1
和r2
的输出值,其中discriminant > 0
- 设置
r1
的输出值,其中discriminant == 0
,而不是r2
的值
- 未设置
r1
,或r2
满足上述条件none。
由于被调用的方法需要赋值,你必须设置r1
和r2
在每个执行路径中。
由于您已将值定义为可空类型,因此您可以使用一些默认值开始您的方法来解决您的问题:
static public int getRealRoots(double A, double B, double C, out double? r1, out double? r2)
{
r1 = null;
r2 = null;
// ... your method code
}
然后在您设置的特定 IF 条件下覆盖默认值。