如何修复}预期的错误? [英] How to fix } expected error ?

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

问题描述

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");
                                                        }



        }
    }
}

推荐答案

SA Kryukov的评论尽管如此,可能还没有告诉你有关系统命名空间的信息。但他对你的代码结构的评论是完全正确的。你需要考虑破坏事物,重用代码,共同元素等等。更好的方法来做你想要的就是为你的月份名称使用一个数组,并为你的消息使用一个通用的模板;类似于:

SA Kryukov's comments notwithstanding, it may be that you have not been told about system namespaces yet. But he is quite correct in his comments about your code structure. you need to think in terms of breaking things down, reuse of code, common elements etc. A much better way to do what you want is to use an array for your month names, and a common template for your message; something like:
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]));
    }
}


每个左括号都必须有一个匹配的右括号。所有其他语句都没有右关闭支架。



int monthNumber; 之前的左括号是必要的。



而不是这个if-else-monstrosity,你应该使用switch语句或字符串数​​组。即使你没有,你也可以这样更可读:



Every left bracket has to have a matching right bracket. All your else-statements are lacking the closing right bracket.

The left bracket right before int monthNumber; is unneccessary.

Instead of this if-else-monstrosity you should use a switch-statement or a string array. Even if you didn't, you could make this much more readable like this:

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..


你可以通过不写任何东西来修复它类似,永远。嵌套的if,硬编码立即常量 - 不仅这是完全不可维护的,而且很难看到。



全部月份名称已在系统中。每个月都不是单独的情况,它只是代码的一小段,不依赖于月份值。这是一个简单的想法:创建一个 System.DateTime 的实例,并将month属性设置为所需的值,它们使用 System.DateTime.ToString对其进行格式化(字符串格式)。请参阅:

DateTime构造函数(Int32, Int32,Int32)(系统) [ ^ ](年份和日期无关紧要,使用任何,指定月份),

DateTime.ToString方法(字符串)(系统) [ ^ ]。



对于格式,请使用月份的全名格式,即MMMM。此处说明自定义格式:自定义日期和时间格式字符串 [ ^ ]。



这样,你不需要任何if或switch-case语句,或者只需要一个,检查值是否为范围从1到12.



这就是全部。在问我更多详细信息之前,请亲自尝试一下;这太简单了。



-SA
You can fix it by not writing anything similar, ever. Nested "if", hard-coded immediate constants — not only this is totally non-maintainable, but is pain to see.

All the month names are already in the system. Each of the months is not the separate case, it's just one short fragment of code, not depending on month value. Here is the simple idea: create an instance of System.DateTime and set a month property to required value, them format it using System.DateTime.ToString(string format). Please see:
DateTime Constructor (Int32, Int32, Int32) (System)[^] (year and day don't matter, use any, with specified month),
DateTime.ToString Method (String) (System)[^].

For format, use "full name of the month" format, which is "MMMM". Custom formats are explained here: Custom Date and Time Format Strings[^].

This way, you won't need any "if" or "switch-case" statements, or perhaps just one, checking up if the value is withing the range 1 to 12.

That's all. Please, before asking me about further detail, try to do it all by yourself; it's way too simple.

—SA


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆