Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法和Math.Max从用户提示返回值_C#_Class_Methods - Fatal编程技术网

C# 方法和Math.Max从用户提示返回值

C# 方法和Math.Max从用户提示返回值,c#,class,methods,C#,Class,Methods,我试图通过请求用户输入第一个和第二个值来返回最大值。我还试图利用Math.Max函数 class Return_Values { public void RunExercise() { Console.WriteLine("First value?"); int firstNumber = Int32.Parse(Console.ReadLine()); Console.WriteLine("Second value?");

我试图通过请求用户输入第一个和第二个值来返回最大值。我还试图利用Math.Max函数

class Return_Values
{
    public void RunExercise()
    {
        Console.WriteLine("First value?");
        int firstNumber = Int32.Parse(Console.ReadLine());

        Console.WriteLine("Second value?");
        int secondNumber = Int32.Parse(Console.ReadLine());

        int theMax;

        Max m = new Max();
        m.returnMax(firstNumber, secondNumber);                                       
        Console.WriteLine("The max of {0} and {1} is {2}", firstNumber, secondNumber, theMax);

    }
}

class Max
{
    public int returnMax(int firstNumber, int secondNumber)
    {
        int theMax = Math.Max(firstNumber, secondNumber);
        return theMax;
    }
}

我不断得到错误,使用未分配的局部变量“theMax”。

您忘记实际将最大值分配给void返回变量

int theMax;

Max m = new Max();
theMax = m.returnMax(firstNumber, secondNumber); 

这个错误是不言自明的。最大值未分配。
m.returnMax(第一个数字,第二个数字)
返回一个
int
,但您没有捕获它!尝试
int theMax=m.returnMax(第一个数字,第二个数字)