没有重载方法' conversationOutput'需要' 2'参数

时间:2016-03-18 04:08:36

标签: c# methods arguments overloading

您好我遇到错误"没有重载方法' conversationOutput'需要' 2'参数"请帮忙。我在理解方法调用方面遇到了一些麻烦,并且从main()调用变量。

class MainClass
{

    static void Main ()
    {
        string selection;
        double values;

        selection = userChoice(); 

        values = userInput(selection);  

        conversationOutput(selection, values);


    }

    static string userChoice ()
    {

        string choose;

        Console.WriteLine("Welcome to the OHM's law calculator! \n \nWhat would you like to calcualte?");
        Console.WriteLine("1. Voltage:");
        Console.WriteLine("2. Current:");
        Console.WriteLine("3. Resistance:");
        do
        {
            choose = Console.ReadLine();
            if (choose != "1" && choose != "2" && choose != "3")
            {
                Console.WriteLine("Please only select '1' for voltage, '2' for current, '3' resistance");
            }
        } while (choose != "1" && choose != "2" && choose != "3");

        switch (choose)
        {
            case "1":
                Console.Write("You have choose to calculate Voltage.\n");
                break;

            case "2":
                Console.Write("You have choose to calculate Current.\n");
                break;

            case "3":
                Console.WriteLine("You have choose to calculate Resistance.\n");
                break;

        }
        return choose;

    }

    static double userInput (string choose)
    {

        double voltageD = 0.0;
        double currentD = 0.0;
        double resistanceD = 0.0;
        string current;
        string voltage;
        string resistance;
        bool ok = true;


        do if (choose != "1")
        {
            do
            {
                Console.WriteLine("Please enter your value for Voltage (volts)...");
                voltage = Console.ReadLine();
                ok = double.TryParse(voltage, out voltageD);
                if (!ok)
                {
                    Console.WriteLine("Please Enter a NUMERICALE VALUE ONLY");
                }
            } while (!ok);

            if (voltageD < 0)
            {
                Console.WriteLine("Please select a number larger than the lower limit of 0 volts");
            }
        } while (voltageD < 0);



        do if (choose != "2")
        {
            do
            { 
                Console.WriteLine("Please enter your value for Current (amp)...");
                current = Console.ReadLine();
                ok = double.TryParse(current, out currentD);
                if (!ok)
                {
                    Console.WriteLine("Please Enter a NUMERICALE VALUE ONLY");
                }
            } while (!ok);
            if (currentD < 0.01)
            {
                Console.WriteLine("Please select a number larger than the lower limit of 0.01 amp");
            }
        } while (currentD < 0.01);



        do if (choose != "3")
        {
            do
            {
                Console.WriteLine("Please enter your value for Resistance (ohm)...");
                resistance = Console.ReadLine();
                ok = double.TryParse(resistance, out resistanceD);
                if (!ok)
                {
                    Console.WriteLine("Please Enter a NUMERICALE VALUE ONLY");
                }
            } while (!ok);
            if (resistanceD < 10)
            {
                Console.WriteLine("Please select a number larger than the lower limit of 10 ohm");
            }
        } while (resistanceD < 10);
        return 0;
    }

    static double conversationOutput (string choose, double currentD, double voltageD, double resistanceD)
    {
        /* v = i*r */

        double output = 0.0;
        string units = "";

        if (choose == "1")
        {
            output = (currentD) * (resistanceD);
            units = "Volts";
        }

        if (choose == "2")
        {
            output = (voltageD) / (resistanceD);
            units = "Amps";
        }

        if (choose == "3")
        {
            output = (voltageD) / (resistanceD);
            units = "OHM";
        }


        Console.WriteLine("The calculated value is {0:F3} {1:F3}", output, units);
        return 0;

    }
}

3 个答案:

答案 0 :(得分:0)

原因是你有一个方法

double conversationOutput (string choose, double currentD, double voltageD, double resistanceD)

有4个参数,你只传递2个参数。

答案 1 :(得分:0)

将缺少的参数定义为可选

static double conversationOutput (string choose, double currentD, double voltageD =0, double resistanceD =1) 

答案 2 :(得分:0)

根据方法定义,conversationOutput需要四个参数。

static double conversationOutput (string choose, double currentD, double voltageD, double resistanceD) {}

但是你通过传递两个参数来调用它。

conversationOutput(selection, values);

这就是为什么你会收到这样的错误。

如果通过传递四个参数调用conversationOutput方法,它将起作用。