【问题标题】:Difficulty returning some values in C#难以在 C# 中返回某些值
【发布时间】:2017-01-15 20:33:34
【问题描述】:

所以我要简化我正在处理的任务。对不起,如果这太糟糕了,我对 C# 很陌生。我应该补充一点,finalMethod 调用在 Main() 中,并且是 Main() 中唯一的东西。

finalMethod(ifPossible(functionOne()), functionTwo()))

static int functionOne()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

static int functionTwo()
    {
        int number;
        Console.WriteLine("Enter a number: ");
        number = int.Parse(Console.ReadLine());
        return number;
    }

static bool ifPossible(int x, int y)
    {
        if (x < y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

static int finalMethod(bool x)
    {
        if (x == true)
        {
            Console.Write("Success");
        }
        else
        {
            Console.Write("Fail");
        }
    }

我的问题是我需要将前两个函数的 int 值返回到 finalMethod 函数中。这可能需要进行大量重组,但我们将不胜感激。

【问题讨论】:

  • 不清楚你在问什么。为什么不从finalMethod() 调用ifPossible() 并将xy 都传递给finalMethod()? Stack Overflow 上有很多答案已经在讨论如何从单个方法返回多个值,但这里似乎甚至不需要。你试过什么?您具体遇到了什么问题?

标签: c# methods int return


【解决方案1】:

您在错误的地方关闭了)。试试这个:

finalMethod(ifPossible(functionOne(), functionTwo()));

尝试像这样进行一些重构:

static int getInputValue()
{
    Console.WriteLine("Enter a number: ");
    var input = Console.ReadLine();
    return int.Parse(input);
}

static bool ifPossible(int x, int y)
{
    return x < y;
}

static void finalMethod(bool x)
{
    if (x)
    {
        Console.Write("Success");
    }
    else
    {
        Console.Write("Fail");
    }
}

var number1 = getInputValue();
var number2 = getInputValue();

finalMethod(ifPossible(number1, number2))

【讨论】:

    【解决方案2】:

    上面代码中的几件事: 1.从finalmethod中移除int返回值 2. ifpossible 采用 1 个参数,它应该采用 2 个。

        class Program
        {
            static void Main(string[] args)
            {
                SomeClass.finalMethod(SomeClass.ifPossible(SomeClass.functionOne(), SomeClass.functionTwo()));
                Console.ReadLine();
            }
    }
        class SomeClass
    {
        internal static int functionOne()
        {
            int number;
            Console.WriteLine("Enter a number: ");
            number = int.Parse(Console.ReadLine());
            return number;
        }
    
        internal static int functionTwo()
        {
            int number;
            Console.WriteLine("Enter a number: ");
            number = int.Parse(Console.ReadLine());
            return number;
        }
    
        internal static bool ifPossible(int x, int y)
        {
            if (x < y)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        internal static void finalMethod(bool x)
        {
            if (x)
            {
                Console.Write("Success");
            }
            else
            {
                Console.Write("Fail");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多