简体   繁体   English

嵌套循环,如何制作日历

[英]nested loops, how to make a calendar

Hello I am a beginner and I am getting very angry at this problem because I can't quite figure out. 你好我是初学者,我对这个问题非常生气,因为我无法弄明白。 The question is to make a calendar using NESTED LOOPS, can anyone help me out? 问题是使用NESTED LOOPS制作日历,任何人都可以帮助我吗? The calendar should somewhat look like the one below, dates should be corresponding to the day of week and should also look like how a calendar looks like (spaces, structure...) 日历应该看起来像下面的日历,日期应该对应于星期几,也应该看起来像日历的样子(空格,结构......)

      Su   M   T   W   Th   F  Sa 
                   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

All i know is how to make columns and rows filled with "x's". 我所知道的是如何使用“x”填充列和行。

public class sumfOddsInNumber
{
    public static void main(String[] args)
    {
        for (int r = 1; r <= 6; r++)
        {
            for(int c = 1; c <= 7; c++)
            {
                System.out.print("x");
            }
            System.out.println();
        }
    }
}

Seems like this is a homework problem, so I'm not going to give you the code, but you're heading in the right direction. 这似乎是一个家庭作业问题,所以我不打算给你代码,但你正朝着正确的方向前进。 First, I'd change 首先,我改变了

System.out.print("x");

to

System.out.print("  x"); //Add two spaces in front of the x

so you have space between the numbers. 所以你在数字之间有空格。 Next, to generate actual numbers instead of x's, put int dayOfMonth = 1; 接下来,要生成实际数字而不是x,请将int dayOfMonth = 1; above the for loop. for循环之上。 You'll then want to print out dayOfMonth instead of x's. 然后,您将要打印出dayOfMonth而不是x。 The problem I leave to you is how to make the value of dayOfMonth increase each time. 我留给你的问题是如何使dayOfMonth的值dayOfMonth增加。

This is not really a programming issue, it is a question of logic. 这不是一个编程问题,它是一个逻辑问题。 If you would concentrate for about 4 minutes straight, you would have figured it out. 如果你能集中精力大约4分钟,你就会想出来。 But I guess nobody takes their time with homework anymore. 但我想没有人会花时间做作业了。 This is how bad programmers are born, please learn to have more ambition than a can opener. 这就是程序员出生的糟糕程度,请学会比开罐器更有野心。


I've made you a small, stylish example, that does exactly what you're asking for. 我已经为你做了一个小巧,时尚的例子,它完全符合你的要求。

Code isn't optimized. 代码未优化。 I just left it like I've thought it (yup, 4 minutes). 我就像我想的那样离开它(是的,4分钟)。 Please take the time to review and improve this example. 请花点时间来审核并改进此示例。 Everything is explained by comments. 一切都由评论解释。

/**
 * The parameters indicate where the month starts,
 * and where it ends.
 *
 * @author ggrec
 *
 */
public class Calendar 
{
    private static final String WEEKDAYS = "Su Mo Tu We Th Fr Sa";
    private static final String NEW_LINE = "\n";
    private static final String EMPTY_STRING = " ";
    private static final String TRIPLE_EMPTY_STRING = "   ";

    public static void main(final String[] args) 
    {
        final String calendarString = getFormattedCalendar(4, 6);

        System.out.println(calendarString);
    }

    private static String getFormattedCalendar(final int startDay, final int endDay)
    {
        // Create StringBuilder
        final StringBuilder calendar = new StringBuilder();

        // Append weekdays to string header
        calendar.append(WEEKDAYS).append(NEW_LINE);

        // This will keep track of days
        int day = 1;

        for (int i = 1; i <= 5; i++) // Week loop
        {
            for (int j = 1; j <= 7; j++) // Weekday loop
            {
                // If we are on the last week of the month,
                // and we've reached the endDay that we specified,
                // simply return the assembled string
                if (i == 5 && j == endDay + 1)
                    return calendar.toString();

                // These are the empty spaces for the beginning of
                // the first week
                if (i == 1 && j < startDay)
                {
                    // Just append empty space, then CONTINUE
                    // to next iteration (j++)
                    calendar.append(TRIPLE_EMPTY_STRING);
                    continue;
                }

                // Check if the day is a single or double digit
                if (day / 10 >= 1)
                    calendar.append(day++).append(EMPTY_STRING);
                else
                    // If this is the first week, then it means that
                    // we have single-digit days. Apply strings on each
                    // side of the day for proper spacing of digits
                    calendar.append(EMPTY_STRING).append(day++).append(EMPTY_STRING);
            }

            calendar.append(NEW_LINE);
        }

        return calendar.toString();
    }
}

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

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