这不是真正的编程问题,而是逻辑问题。如果你连续集中注意力大约 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();
}
}