方法从一个传递到另一个

时间:2014-11-28 10:50:59

标签: c# function

这基本上是我想要完成的一个愚蠢的版本。 我试图告诉程序将两个方法从不同的函数传递到最后一个但是它表示它们在当前上下文中不存在,即使我试图在Num1和Num2中传递它们

static void Main(String[] args)
{
    int Option;
    DisplayMenu();
    Option = GetUserOption();
    while (Option != 0)
    {
        switch (Option)
        {
            case 1: 
                Num1();
                break;
            case 2:
                Num2();
                break;
            case 3: 
                Overall(Num3, Num4);
                break;
        }
    }
}

static void DisplayMenu()
{
    Console.WriteLine("1. Num1 2.Num2 3.Overall");
}

static int GetUserOption()
{
    int Option;
    Console.WriteLine("Pick choice");
    Option = Convert.ToInt32(Console.ReadLine());
    return Option;
}

static int Num1()
{
    int Num3;
    Console.WriteLine("Enter your first number");
    Num3 = Convert.ToInt32(Console.ReadLine());
    return Num3;
}

static int Num2()
{
    int Num4;
    Console.WriteLine("Enter your second number");
    Num4 = Convert.ToInt32(Console.ReadLine());
    return Num4;
}

public static int Overall(int Num3, int Num4)
{
    int Overall;
    Console.WriteLine("This will add the two together");
    Overall = Overall + Num3 + Num4;
    Console.WriteLine(Overall);
    return Overall;
}

它仍然告诉我,在当前的背景下,params不存在。

编辑:将参数移动到整体情况但仍然是相同的错误消息。

4 个答案:

答案 0 :(得分:0)

我可以看到一些问题:

  1. Main中,您尚未声明变量Num3Num4,因此编译器不满意您尝试将它们传递给{{1} }} 方法。您应该在循环之前声明它们(并将它们初始化为某个默认值)。

  2. {li>

    OveralliNum3iNum4方法中不存在。您已将参数命名为OverallNum3

  3. 您已在方法Num4中声明了一个名为Overall的变量,但尚未对其进行初始化,因此当您尝试执行Overall时,变量不会但有价值。写Overall + iNum3 + iNum4

  4. 更简单

答案 1 :(得分:0)

首先,这不是你怎么做这样的事情,它是非常复杂的,没有意义(花了一些时间来弄清楚你在做什么)。我仍然不知道你是否正在努力做我正在思考的事情。

无论如何,我想你想要将两个数字加在一起,如果他想输入数字1,数字2或显示当前结果,每个步骤都给用户一个选项?这是一个解决方案,我试图保持你的风格(包括你正在学习开关和功能),同时保持代码。它仍然是“复杂”的方式,因为它正在做什么

static void Main(String[] args)
    {
        int option = 1;
        int num1 = 0;
        int num2 = 0;
        int sum = 0;
        DisplayMenu();
        while (option != 0)
        {
            option = GetUserOption();
            switch (option)
            {
                case 1:
                    num1 = getNum(option);
                    break;
                case 2:
                    num2 = getNum(option);
                    break;
                case 3:
                    Overall(num1, num2, sum);
                    break;
            }
        }
    }

    static void DisplayMenu()
    {
        Console.WriteLine("1: Num1, 2: Num2, 3: Overall, 0: Exit");
    }

    static int GetUserOption()
    {
        Console.WriteLine("Pick choice");
        return Convert.ToInt32(Console.ReadLine());
    }

    static int getNum(int option)
    {
        if (option == 1)
            Console.WriteLine("Enter your first number");
        else
            Console.WriteLine("Enter your second number");
        return Convert.ToInt32(Console.ReadLine());
    }

    static int Overall(int num1, int num2, int sum)
    {
        Console.WriteLine("This will add the two together");
        sum = num1 + num2;
        Console.WriteLine(sum);
        return sum;
    }

答案 2 :(得分:0)

using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;  
namespace ConsoleApplication1
{
class Program
    {
    static void Main(String[] args)
        {
        int Option;
        int Num3, Num4;
        Num3=1;
        Num4 = 3;
        DisplayMenu();
        Option = GetUserOption();
        while (Option != 0)
            {
            switch (Option)
                {
                case 1:
                    Num1();
                    break;
                case 2:
                    Num2();
                    break;
                case 3:
                    Overall(Num3, Num4);
                    break;
                }
            }
        }

    static void DisplayMenu()
        {
        Console.WriteLine("1. Num1 2.Num2 3.Overall");
        }

    static int GetUserOption()
        {
        int Option;
        Console.WriteLine("Pick choice");
        Option = Convert.ToInt32(Console.ReadLine());
        return Option;
        }

    static int Num1()
        {
        int Num3;
        Console.WriteLine("Enter your first number");
        Num3 = Convert.ToInt32(Console.ReadLine());
        return Num3;
        }

    static int Num2()
        {
        int Num4;
        Console.WriteLine("Enter your second number");
        Num4 = Convert.ToInt32(Console.ReadLine());
        return Num4;
        }

    public static int Overall(int Num3, int Num4)
        {
        int Overall=0;
        Console.WriteLine("This will add the two together");
        Overall = Overall + Num3 + Num4;
        Console.WriteLine(Overall);
        return Overall;
        }
    }
}

答案 3 :(得分:0)

问题出在这里,编译器不知道你的意思是哪个Overall

static int Overall(int Num3, int Num4)
{
    int Overall;
    Console.WriteLine("This will add the two together");
    Overall = Overall + iNum3 + iNum4;
    Console.WriteLine(Overall);
    return overall;
}

以小写Overall

制作内部范围overall
static int Overall(int Num3, int Num4)
{
    int overall;
    Console.WriteLine("This will add the two together");
    overall= overall + iNum3 + iNum4;
    Console.WriteLine(overall);
    return Overall;
}