2010-03-06 255 views
1

我正在做這個簡單的代碼片段來使用日期編號生成月份名稱,但我得到了錯誤Cannot implicitly convert type 'int' to 'string'。我不是想把MonthName改成int!錯誤「無法將類型'int'隱式轉換爲'字符串'」

class Module04 
{ 
    public static void Exercise01() 
    { 
     Console.WriteLine("Please Enter a day number between 1 and 365: "); 
     string line = Console.ReadLine(); 
     int dayNum = int.Parse(line); 

     int monthNum = 0; 

     if (dayNum <= 31) // January 
     { 
      goto End; 
     } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 28) // February 
     { 
      goto End; 
     } 
     else 
     { 
      dayNum -= 28; 
      monthNum++; 
     } 

     if (dayNum <= 31) // March 
     { 
      goto End; 
     } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 30) // April 
     { goto End; } 
     else 
     { 
      dayNum -= 30; 
      monthNum++; 
     } 

     if (dayNum <= 31) // May 
     { goto End; } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 30) // June 
     { goto End; } 
     else 
     { 
      dayNum -= 30; 
      monthNum++; 
     } 

     if (dayNum <= 31) // July 
     { goto End; } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 31) // August 
     { goto End; } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 30) // September 
     { goto End; } 
     else 
     { 
      dayNum -= 30; 
      monthNum++; 
     } 

     if (dayNum <= 31) // October 
     { goto End; } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

     if (dayNum <= 30) // November 
     { goto End; } 
     else 
     { 
      dayNum -= 30; 
      monthNum++; 
     } 

     if (dayNum <= 31) // December 
     { goto End; } 
     else 
     { 
      dayNum -= 31; 
      monthNum++; 
     } 

    End: 

     string monthName; 

     switch (monthName) 
     { 
      case 0: 
       monthName = "January"; break; 
      case 1: 
       monthName = "February"; break; 
      case 2: 
       monthName = "March"; break; 
      case 3: 
       monthName = "April"; break; 
      case 4: 
       monthName = "May"; break; 
      case 5: 
       monthName = "June"; break; 
      case 6: 
       monthName = "July"; break; 
      case 7: 
       monthName = "August"; break; 
      case 8: 
       monthName = "September"; break; 
      case 9: 
       monthName = "October"; break; 
      case 10: 
       monthName = "November"; break; 
      case 11: 
       monthName = "December"; break; 
      default: 
       monthName = "Not yet Done"; break; 
     } 
     Console.WriteLine("{0} {1}", dayNum, monthName); 
    } 
} 
+0

此代碼沒有考慮閏年。 – 2010-03-06 05:23:40

+0

不應該有這個作業標籤嗎? – 2010-03-06 06:20:12

+0

oopz .. ddnt knw abt it ..我是一個新手在這裏.. il標籤t 4肯定nxt ym:D – user282659 2010-03-06 06:38:03

回答

2

這是你使用它作爲一個int其中switch - 你大概意思傳遞給monthNum switch語句。

(另外:你不應該有monthNum == ...在所有if S IN的第一部分的?)

1

更改開關

switch (monthNum) 
+0

解決THankss:D – user282659 2010-03-06 05:31:43

0

MONTHNAME是一個字符串,那麼你的交換機的情況下應該是一個字符串也。

string monthName; 

      switch (monthName) 
      { 
       case "0" : 
        monthName = "January"; break; 
       case "1" : 
        monthName = "February"; break; 
       case "2" : 
        monthName = "March"; break; 
       case "3" : 
        monthName = "April"; break; 
       case "4" : 
        monthName = "May"; break; 
       case "5" : 
        monthName = "June"; break; 
       case "6" : 
        monthName = "July"; break; 
       case "7" : 
        monthName = "August"; break; 
       case "8" : 
        monthName = "September"; break; 
       case "9" : 
        monthName = "October"; break; 
       case "10" : 
        monthName = "November"; break; 
       case "11" : 
        monthName = "December"; break; 
       default : 
        monthName = "Not yet Done"; break; 
      } 
     Console.WriteLine("{0} {1}", dayNum, monthName); 
    } 
相關問題