无法将类型'int'隐式转换为'string'。 CS0029

时间:2017-09-28 09:47:52

标签: c# visual-studio-2017

我的代码出现问题。我不知道如何修复此错误,因此非常感谢您的帮助。

    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 个答案:

答案 0 :(得分:4)

int Number, season;

这是错误的,因为数字变量是整数类型,但season变量不能是整数

你必须像这样定义

int Number;    
string season = string.Empty;