嵌套循环,如何制作日历

时间:2013-09-03 00:15:32

标签: java loops

您好我是初学者,我对这个问题非常生气,因为我无法理解。问题是使用NESTED LOOPS制作日历,任何人都可以帮助我吗?该 日历应该看起来像下面的那个,日期应该对应于星期几,也应该看起来像日历的样子(空格,结构...)

      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

我所知道的是如何使用“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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

似乎这是一个家庭作业问题,所以我不打算给你代码,但你正朝着正确的方向前进。首先,我要改变

System.out.print("x");

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

所以你在数字之间有空格。接下来,要生成实际数字而不是x,请将int dayOfMonth = 1;放在for循环之上。然后,您需要打印dayOfMonth而不是x。我留给您的问题是如何使每次dayOfMonth的值增加。

答案 1 :(得分:1)

这不是一个编程问题,而是一个逻辑问题。如果你能集中精力大约4分钟,你就会想出来。但我想没有人会花时间做作业了。这就是程序员出生的糟糕程度,请学会比开罐器更有野心。


我已经为你做了一个小巧,时尚的例子,它完全符合你的要求。

代码未优化。我就像我想的那样离开它(是的,4分钟)。请花点时间来审核并改进此示例。一切都由评论解释。

/**
 * 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();
    }
}