简体   繁体   English

带有不同月份选项的简单日历

[英]Simple Calendar with option for different months

Here's the prompt: Write a program that prints a calendar for one month. 提示如下:编写一个程序,打印一个月的日历。 Input consists of an integer specifying the first day of the month (1 = Sunday) and an integer specifying how many days are in a month. 输入包含一个整数,该整数指定一个月的第一天(1 =星期日),以及一个整数,指定一个月中有多少天。

Here is a sample output of what it should look like: 这是它的外观示例输出:

    First day of the month 3 
    Number of days in the month 31

    Sunday Monday Tuesday Wednesday Thursday Friday Saturday
                     1        2        3       4       5 
      6      7       8        9        10      11      12
      13     14      15       16       17      18      19
      20     21      22       23       24      25      26
      27     28      29       30       31

I've been working on it and here is what I have so far. 我一直在努力,这是到目前为止的成果。

namespace Program_202t
{
   class Program
   {

     static void Main(string[] args)
     {
        Console.Title = "Program 202t";

        Console.Write("Enter the first day of the month: ");
        int year = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of days in a month: ");
        int month = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("\nSunday  Monday   Tuesday  Wedsnesday  Thursday  Friday  Saturday");
        if (year == 1)
        {
            Console.SetCursorPosition(0, 4);
            for (int i = 1; i <= 10; i++)
            {
                Console.Write(i + "         ");


                if (((i) % 7) > 0)
                {
                }

                else
                {
                    Console.Write("\n");
                }
            }
            Console.Write("\b");
            for (int b = 11; b <= month; b++)
            {
                if (((b) % 7) > 0)
                {
                    Console.Write(b + "        ");
                }
                else
                {
                    Console.Write(b + "\n");
                }

            }

        }
        if (year == 2)
        {
            //Console.SetCursorPosition(10, 4);
            for (int i = 1; i <= month; i++)
            {
                if (i == 1)
                {
                    Console.Write("         " + i + "         ");
                }
                else if (i > 1 && i < 11 && i != 6)
                {
                    Console.Write(i + "         ");
                }
                Console.Write("\b");
                if (i == 6)
                {
                    Console.Write(i + "\n");
                }
                if (i > 11 && i != 14)
                {
                    Console.Write(i + "       ");
                }
                if (i == 14)
                {
                    Console.Write(i + "\n");
                }


                /*if (((i) % 6) > 0)
                {
                }

                else
                {
                    Console.Write("\n");
                }
            }
            Console.Write("\b");
            //FIX!!!
            for (int b = 11; b <= month; b++)
            {
                if (((b) % 13) > 0 && b >13)
                {
                    Console.Write(b + "        ");

                }
                if (((b) % 20) > 0 && b <= 13)
                {
                    Console.Write(b + "        ");
                }
                else
                {
                    Console.Write("\n");
                }

            }*/

            }
            if (year == 3)
            {
                Console.SetCursorPosition(20, 4);
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 5) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write("\n");
                    }

                }

            }
            if (year == 4)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 5)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 6)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            if (year == 7)
            {
                for (int i = 1; i <= 10; i++)
                {
                    Console.Write(i + "         ");


                    if (((i) % 7) > 0)
                    {
                    }

                    else
                    {
                        Console.Write("\n");
                    }
                }
                Console.Write("\b");
                for (int b = 11; b <= month; b++)
                {
                    if (((b) % 7) > 0)
                    {
                        Console.Write(b + "        ");
                    }
                    else
                    {
                        Console.Write(b + "\n");
                    }

                }

            }
            Console.ReadLine();
        }


    }

}

Do any of you know what I am doing wrong for the years that aren't 1? 你们当中有人知道我在不是1年的时间里做错什么吗? One works fine, but the rest have various problems. 一种工作正常,但其余的都有各种问题。

Thanks for the help! 谢谢您的帮助!

Will this do? 这样可以吗

 class Program
{
    static void Main(string[] args)
    {
        Console.Title = "Program 202t";

        Console.Write("Enter the first day of the month: ");
        int startingDay = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the number of days in a month: ");
        int daysInMonth = Convert.ToInt32(Console.ReadLine());

        List<string> daysOfTheWeek = new List<string>() {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        foreach (string day in daysOfTheWeek)
        {
            Console.Write($"{day,10}");
        }
        List<string> days = new List<string>();
        for (int i = 0; i < startingDay; i++)
        {
            days.Add($"{"",10}");
        }
        for (int i = 1; i < daysInMonth+1; i++)
        {
            days.Add($"{i,10}");
        }
        for (int i = 0; i < days.Count; i++)
        {
            if (i%7!=0) {Console.Write(days[i]);}
            else {Console.WriteLine(days[i]);}                
        }

    }

Alrighty, i did a quick run through of the code and I see where you're issues are so I'll give you some tips on where to look. 好吧,我快速浏览了一下代码,然后看到了问题所在,因此我将向您提供一些有关查找位置的提示。

For one, your code has logical issues revolving around the required formatting of the output. 一方面,您的代码存在逻辑问题,涉及到所需的输出格式。 There are simpler ways to output the correct formatting. 有更简单的方法来输出正确的格式。 If you look at the .NET String Reference then look at formatting, you may find an easier way to do it. 如果您查看.NET字符串参考,然后查看格式,则可能会找到一种更简单的方法。

Next, you have an issue with misaligned block endings so some of your code is never getting executed. 接下来,您遇到了块结尾未对齐的问题,因此某些代码永远不会执行。 I recommend you breakpoint your code at the first if statement and step through the code to understand the execution in runtime. 我建议您在第一个if语句处断点代码,并逐步执行代码以了解运行时的执行情况。

Lastly, I recommend clarifying your variable names a little bit. 最后,我建议稍微澄清一下变量名。 If the first input represent that day of the week, then perhaps use a variable name that represents it (for example dayOfWeek , but month doesn't do it for me). 如果第一个输入代表星期几,则可以使用一个代表它的变量名(例如dayOfWeek ,但是month不适用于我)。 Also the number of days in the month could use a better variable name. 同样,一个月中的天数可以使用更好的变量名。

Please do ask questions if you need some more guidance. 如果您需要更多指导,请提出问题。 I'd be happy to help. 我很乐意提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM