C# 使用switch语句确定用户输入的内容

C# 使用switch语句确定用户输入的内容,c#,C#,所以我试图编写一个程序来输入用户的字符串值。我试图使用开关来确定在“a”和“b”之间按下了什么。我一直遇到以下错误:参数1:无法从“方法组”转换为“bool” namespace ConsoleApplication4 { class Program { static void Main(string[] args) { const string a = " You pressed a"; const string b = " You

所以我试图编写一个程序来输入用户的字符串值。我试图使用开关来确定在“a”和“b”之间按下了什么。我一直遇到以下错误:参数1:无法从“方法组”转换为“bool”

namespace ConsoleApplication4
{
    class Program
   {
    static void Main(string[] args)
    {
        const string a = " You pressed a";
        const string b = " You pressed b";

        string input = Console.ReadLine();

        switch(input)
        {
            case a:
                ShowData(a);

                break;

            case b:
                ShowData(b);
                break;

            default:
                Console.WriteLine(" You did not type a or b");
                Console.WriteLine();
                Console.ReadLine();
                break;
        }
    }

     static void ShowData(string a)
    {
        Console.WriteLine(ShowData);
    }

   }
}

尝试以下更正:

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            const string a = " You pressed a";
            const string b = " You pressed b";

            string input = Console.ReadLine();

            switch (input)
            {
                case "a": // correction 1
                    ShowData(a);
                    break;

                case "b": // correction 2
                    ShowData(b);
                    break;

                default:
                    Console.WriteLine(" You did not type a or b");
                    Console.WriteLine();
                    Console.ReadLine();
                    break;
            }
        }

        static void ShowData(string a)
        {
            Console.WriteLine(a); // correction 3
        }

    }
}

您正在将ShowData传递到控制台。我想你是想写
Console.WriteLine(a)而不是
Console.WriteLine(ShowData)

您收到的错误似乎是因为您试图编写ShowData方法

static void ShowData(string a)
    {
        Console.WriteLine(ShowData);
    }
应该是:

static void ShowData(string a)
    {
        Console.WriteLine(a);
    }
我想说,您根本不需要ShowData方法,因为您也可以直接在交换机中编写一个,但这取决于您自己

这将消除错误,但我仍然只得到结果“您没有键入a或b”。这是因为你的案例不正确。因为您正在寻找字符串a,所以您的案例应该是

case "a":
而不是

case a:
更改此选项将获得所需的行为。以下是我最后的代码:

namespace ConsoleApplication4
{
class Program
{
    static void Main(string[] args)
    {
        const string a = " You pressed a";
        const string b = " You pressed b";

        string input = Console.ReadLine();

        switch (input)
        {
            case "a":   //Case changed to "a" instead of a
                ShowData(a);        //Here, we could use Console.writeLine(a) directly if we wanted.
                Console.WriteLine();
                Console.ReadLine();
                break;

            case "b":   //Case changed to "b" instead of b
                ShowData(b);    //Here, we could use Console.writeLine(b) directly if we wanted.
                Console.WriteLine();
                Console.ReadLine();
                break;

            default:
                Console.WriteLine(" You did not type a or b");
                Console.WriteLine();
                Console.ReadLine();
                break;
        }
    }

    static void ShowData(string a)
    {
        Console.WriteLine(a);   //Changed from ShowData to a
    }

}
}

我想你需要
Console.WriteLine(a)
在你的
ShowData
方法中。我先尝试了,有更多的错误。我测试了代码,它成功了,谢谢,我也看到了我的错误所在。很好。实际上,我们也可以键入
大小写“a”
大小写“b”
,因为C中的字符被
包围,而字符串被
包围,但无论如何它不会改变任何东西。。。