2016-03-27 59 views
-1

所以我试图编写一个程序来从用户输入一个字符串值。我正在尝试使用开关来确定“a”和“b”之间被按下的内容。我不断收到以下错误:参数1:无法从'方法组'转换为'布尔'。使用switch语句来确定用户输入是什么

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); 
    } 

    } 
} 
+0

我想你想在'ShowData'方法中使用'Console.WriteLine(a);'。 – juharr

+0

我首先试过,还有更多的错误。 – Abdulhamid

回答

6

尝试用这些修正:

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 
     } 

    } 
} 
+0

我测试了代码,它的工作,谢谢 我也看到我的错误在哪里。 – Abdulhamid

+0

好。其实我们也可以输入'case'a''和'case'b'',因为C#中的字符被'''包围,而''被字符串'''包围,但无论如何它不会改变任何东西...... – Bad

1

你传入ShowData到控制台。我想你的意思是写Console.WriteLine(a);代替Console.WriteLine(ShowData);

2

你所得到的错误似乎是因为你试图写ShowData方法,

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

大概应该是:

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

我会说你根本不需要ShowData方法,因为你可能直接在你的开关中写一个,但这取决于你。

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

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 
    } 

} 
}