2016-03-27 58 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 
    } 

} 
}