简体   繁体   English

调用我的方法时如何解决此错误?

[英]How do i fix this error when calling my Method?

Am trying to call a method to the main method, how do i write it so that it does not display errors? 我正在尝试调用一个方法到main方法,如何编写它以便不显示错误?

its mainly meant to prompt a user to enter a name and the age, i have tried to to put the method under the main method but it ain't working. 它的主要目的是提示用户输入名称和年龄,我试图将方法置于主要方法下,但它不起作用。

namespace Methodss
{
    class Program
    {
        static void Main(string[] args)
        {
            SayHi(String Name, int Age)
        }
        static void SayHi(String Name, int Age)
        {
            Name = Console.ReadLine();
            Console.WriteLine("Enter Your Name");
            Console.ReadKey();
            Age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Your Age");
            Console.ReadKey();
            Console.ReadLine();
            Console.WriteLine("Hello User"+Name+"you are"+ Age+" Years old");
        }
    } 
}

I expect the upon compiling the program to ask for name and age then output "Hello "Name" your "Age" years of age". 我希望在编译程序时询问姓名和年龄,然后输出“您好,“姓名”,您的“年龄”年龄”。 where Name and Age are the Values from what the user inputs 其中“名称”和“年龄”是用户输入的值

Made a few Changes, from the suggestions, it worked namespace Methodss { class Program { static void Main(string[] args) 根据建议进行了一些更改,使其在名称空间中起作用。方法{类Program {静态void Main(string [] args)

    {
        SayHi();
    }
    static void SayHi()
    {
        Console.WriteLine("Enter Your Name");
        var name=Console.ReadLine();
        Console.WriteLine("Enter Your Age");
        int age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Hello \t" + name + " you are " + age + " Years old");
        Console.ReadLine();
    }
}

} }

This how i have redone it, its now working as expected 这是我重做的方式,现在可以正常使用了

namespace Methodss
{
    class Program
    {
        static void Main(string[] args)
        {
            SayHi();
        }

        static void SayHi()
        {
            Console.WriteLine("Enter Your Name");
            var name=Console.ReadLine();
            Console.WriteLine("Enter Your Age");
            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Hello "+"\t" + name + " "+"you are"+" " + age + " "+" Years old");
            Console.ReadLine();
        }
    }
}

The main error is that you are confusing the list of actual parameters with the list of formal parameters. 主要错误是您将实际参数列表与形式参数列表混淆。 Formal parameters contain the information about the type of the parameter, while the actual parameter is a variable or a expression. 形式参数包含有关参数类型的信息,而实际参数是变量或表达式。

For example, the function MultiplyBy2() has a formal parameter of type double , but can be invoked with an expression or a variable. 例如,函数MultiplyBy2()具有形式为double的形式参数,但可以使用表达式或变量来调用。

  class Main {
    static double MultiplyBy2(double x)
    {
        return 2 * x;
    }

    static void Main(String[] args)
    {
        int x = 5;

        Console.WriteLine( MultiplyBy2( 6 ) );     // 12
        Console.WriteLine( MultiplyBy2( x ) );     // 10
    }
  }

Also, it seems you are interested on returning the name and the age, not actually passing them. 另外,您似乎对返回名称和年龄感兴趣,而不是实际通过它们。 My guess is that you would obtain the name and the age from SayHi() and actually say hello from Main() . 我的猜测是,您将从SayHi()获取名称和年龄,并实际上从Main()打招呼。

It is not trivial to return more than one value, so we can use out to indicate that these variables will be modified with the value set in the SayHi() function. 返回多个值并非易事,因此我们可以用out表示将使用SayHi()函数中设置的值修改这些变量。

This also means that SayHi() is not the appropriate name for that function. 这也意味着SayHi()不是该函数的适当名称。 Let's go with AskPersonalData() . 让我们来看看AskPersonalData()

    class Program
    {
        static void Main(string[] args)
        {
            string name;
            int age;

            AskPersonalData( out name, out age );
            Console.WriteLine( "Hello User" + name + "you are" + age + " Years old" );
        }

        static void AskPersonalData(out string name, out int age)
        {
            Console.Write( "Enter Your Name: " );
            name = Console.ReadLine();

            Console.Write( "Enter Your Age: " );
            age = Convert.ToInt32( Console.ReadLine() );
        }
    } 

You can dig deeper about parameter passing in the MSDN . 您可以深入了解MSDN中的参数传递

Hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决尝试调用包含 KeyEventArgs e 作为参数的方法时出现的错误 - How do I fix the error I have when trying to call the method that contain KeyEventArgs e as argument 在非托管DLL中调用方法时,如何保护C#应用程序免于崩溃? - How do I protect my C# app from crashing when calling a method in an unmanaged DLL? System.ArgumentOutOfRangeException 调用方法时。 如何找出错误发生的位置? - System.ArgumentOutOfRangeException when calling Method. How do I find out where the error occurs? 调用方法时如何使用局部变量? - How do I use local variables when calling a method? 我的命名空间错误以及如何修复 RDL 报告? - Error on my namespace and how do i fix it RDL report? 如何修复 Azure Function 上的此 502 错误? - How do I fix this 502 Error on my Azure Function? 当我不断调用基类方法时,不确定如何调用我的具体类方法 - Not sure how to call my concrete class method when I keep calling the base class method 如何使用 unity 的 Object.FindObjectOfType 解决我无法在脚本中找到有效方法的问题? - How do I fix my failure to find a valid method in a script using unity's Object.FindObjectOfType? 如何修复我的linq查询 - How do I fix my linq query 我出现错误“方法'getValidString'的重载不带参数”。 这是什么意思,我该如何解决? - I have the error “No overload for method 'getValidString' takes for arguments”. What does this mean and how do i fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM