本文介绍了如何修复}预期的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace Exercise3
{
    class Program
    {
        static void Main(string[] args)
        {
            {
            int monthNumber;

            //Prompt for number
            Console.Write("Enter a number between 1 and 12");
            monthNumber = int.Parse(Console.ReadLine());

            //Calculations
            if (monthNumber == 1)
            {
                Console.Write("This is the 1st month...January");
            }
            else
            {
                if (monthNumber == 2)
                {
                    Console.Write("This is the 2nd month...February");
                }
                else
                {
                    if (monthNumber == 3)
                    {
                        Console.Write("This is the 3rd month...March");
                    }
                    else
                    {
                        if (monthNumber == 4)
                        {
                            Console.Write("This is the 4th month...April");
                        }
                        else
                        {
                            if (monthNumber == 5)
                            {
                                Console.Write("This is the 5th month...May");
                            }
                            else
                            {
                                if (monthNumber == 6)
                                {
                                    Console.Write("This is the 6th month...June");
                                }
                                else
                                {
                                    if (monthNumber == 7)
                                    {
                                        Console.Write("This is the 7th month...July");
                                    }
                                    else
                                    {
                                        if (monthNumber == 8)
                                        {
                                            Console.Write("This is the 8th month...August");
                                        }
                                        else
                                        {
                                            if (monthNumber == 9)
                                            {
                                                Console.Write("This is the 9th month...September");
                                            }
                                            else
                                            {
                                                if (monthNumber == 10)
                                                {
                                                    Console.Write("This is the 10th month...October");
                                                }
                                                else
                                                {
                                                    if (monthNumber == 11)
                                                    {
                                                        Console.Write("This is the 11th month...November");
                                                    }
                                                    else
                                                    {
                                                        if (monthNumber == 12)
                                                        {
                                                            Console.Write("This is the 12th month...December");
                                                        }



        }
    }
}

推荐答案

class Program
{
    static string months[] = { "January", "February", "March", "April", ...
    static string suffixes[] = { "st", "nd", "rd", "th", ...
    static void Main(string[] args)
    {
        int monthNumber = 0;

        //Prompt for number
        while (monthNumber == 0)
        {
            Console.Write("Enter a number between 1 and 12");
            if (!int.TryParse(Console.ReadLine(), monthNumber)
                monthNumber = 0;
        }
        Console.WriteLine(string.Format("This is the {0}{1} month...{2}", monthNumber, suffixes[monthNumber-1], months[monthNumber-1]));
    }
}


if (monthNumber == 1)
{
    Console.Write("This is the 1st month...January");
}
else if (monthNumber == 2)
{
    Console.Write("This is the 2nd month...February");
}
else if (monthNumber == 3)
{
    Console.Write("This is the 3rd month...March");
}
else if // and so on..



这篇关于如何修复}预期的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:49