简体   繁体   English

调用函数 C#

[英]Calling a function c#

This piece of code is suppose to get a letter from the user in 'alphabet' and return the corresponding letter in mapsTo.这段代码假设从用户那里得到一个字母 'alphabet' 并在 mapsTo 中返回相应的字母。 eg 'A' will result in 'Z'例如“A”将导致“Z”

class Program
      {
        static void Main()
        {
        }
        char[] alphabet = { 'A', 'B', 'C' }; 
        char[] mapsTo = { 'Z', 'Y', 'X' };
        public string changeLetter(char input)

        {
            int i = 0;

            foreach (char c in alphabet)
            {
                if (c == input)
                {
                    return mapsTo[i].ToString();
                }
               i++;
            }
            return default(char).ToString();
        }
    }
    }

I know that the code below will store the user input, but I am unsure what is next, and where in the program that code should be placed我知道下面的代码将存储用户输入,但我不确定接下来是什么,以及该代码应该放在程序中的哪个位置

Console.WriteLine("Please enter a number");
            userInput= Console.ReadLine;

Any help appreciated, thanks任何帮助表示赞赏,谢谢

A good place to start would be the Main-method i guess.我想,一个不错的起点是 Main 方法。 When you run the application the Main-method will be called.当您运行应用程序时,将调用 Main-method。 If you put your writeline and userInput variable there, you would be able to call your changeLetter method with userInput as argument.如果你把你的 writeline 和 userInput 变量放在那里,你就可以用 userInput 作为参数调用你的 changeLetter 方法。 Just store the return statement as a new variable and go from there只需将 return 语句存储为一个新变量并从那里开始

You forgot the parentheses:你忘了括号:

userInput = Console.ReadLine();

Also, your main code goes in Main() , like this:此外,你的主要代码放在Main() ,就像这样:

static void Main()
{
    // code here
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM