【问题标题】:Methods and Math.Max to return the value from user prompt [closed]从用户提示返回值的方法和 Math.Max [关闭]
【发布时间】:2019-02-03 19:44:34
【问题描述】:

我试图通过要求用户输入第一个和第二个值来返回最大值。我也在尝试使用 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”。

【问题讨论】:

标签: c# class methods


【解决方案1】:

您忘记将 theMax 实际分配给 void 返回变量

int theMax;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2023-02-19
    相关资源
    最近更新 更多