我的代碼有問題。我不知道如何解決這個錯誤,所以你的幫助將非常感激。不能將類型'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,它不能轉換爲一個字符串。但如果我錯了,請糾正我。 感謝您的幫助:)
的
season
變量不能是整數,這是錯誤的'int Number;字符串季節;' – Backs您將'season'定義爲int:'int Number,season;'。但是你正在給它分配一個字符串。將它聲明爲'string season' –
'int Number,season;'將'Number'和'season'聲明爲int:使用'int Number;串季節;'而是。 –