2017-09-28 126 views
-7

我的代碼有問題。我不知道如何解決這個錯誤,所以你的幫助將非常感激。不能將類型'int'隱式轉換爲'string'。 CS0029

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApp10 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int Number, season; 


      Console.WriteLine("Enter a value of a mounth of the year: (E.G. 1 for January, 2 for Febuary)"); 
      Number = Int32.Parse(Console.ReadLine()); 

      switch (Number) 
      { 
       case 1: Console.WriteLine("January"); break; 
       case 2: Console.WriteLine("Febuary"); break; 
       case 3: Console.WriteLine("March"); break; 
       case 4: Console.WriteLine("April"); break; 
       case 5: Console.WriteLine("May"); break; 
       case 6: Console.WriteLine("June"); break; 
       case 7: Console.WriteLine("July"); break; 
       case 8: Console.WriteLine("Augest"); break; 
       case 9: Console.WriteLine("September"); break; 
       case 10: Console.WriteLine("October"); break; 
       case 11: Console.WriteLine("November"); break; 
       case 12: Console.WriteLine("December"); break; 
      } 

      if (Number == 1) 
      { 
       season = "Winter"; 
      } 
      else if (Number == 2) 
      { 
       season = "Winter"; 
      } 
      else if (Number == 3) 
      { 
       season = "Spring"; 
      } 
      else if (Number == 4) 
      { 
       season = "Spring"; 
      } 
      else if (Number == 5) 
      { 
       season = "Spring"; 
      } 
      else if (Number == 6) 
      { 
       season = "Summer"; 
      } 
      else if (Number == 7) 
      { 
       season = "Summer"; 
      } 
      else if (Number == 8) 
      { 
       season = "Summer"; 
      } 
      else if (Number == 9) 
      { 
       season = "Autumn"; 
      } 
      else if (Number == 10) 
      { 
       season = "Autumn"; 
      } 
      else if (Number == 11) 
      { 
       season = "Autumn"; 
      } 
      else if (Number == 12) 
      { 
       season = "Winter"; 
      } 
      else 
      { 
       Console.WriteLine("Please enter a valad value for a month (i.e. 1-12)"); 
      } 
      Console.WriteLine("The seaon is:\t" + season); 
      Console.ReadLine(); 

     } 
    } 
} 

以上是我寫的代碼。在季節出現的'if'聲明的每個部分都有CS0029錯誤。我有一個理解,這是因爲我使用了一個int,它不能轉換爲一個字符串。但如果我錯了,請糾正我。 感謝您的幫助:)

+1

season變量不能是整數,這是錯誤的'int Number;字符串季節;' – Backs

+1

您將'season'定義爲int:'int Number,season;'。但是你正在給它分配一個字符串。將它聲明爲'string season' –

+3

'int Number,season;'將'Number'和'season'聲明爲int:使用'int Number;串季節;'而是。 –

回答

-1
int Number, season; 

因爲數量變量是整數的類型,但是

你必須這樣定義

int Number;  
string season = string.Empty; 
+0

我改變了它,但現在我有一個錯誤代碼CS0165。 「使用未分配的本地變量」季節「。」那是什麼意思? – FSCockers

+0

老兄,如果它是字符串,你必須分配的值。 請按照我的代碼 「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「」「 「」「」string season = string.Empty;「」「」「」「」「」「」「」「 –

+0

感謝您的幫助。 – FSCockers

相關問題