Java 嵌套循环,如何制作日历
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18582336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nested loops, how to make a calendar
提问by Can't see me
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? 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();
}
}
}
回答by musical_coder
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;
above the for
loop. You'll then want to print out dayOfMonth
instead of x's. The problem I leave to you is how to make the value of dayOfMonth
increase each time.
所以你在数字之间有空格。接下来,要生成实际数字而不是 x,请将其放在循环int dayOfMonth = 1;
上方for
。然后你会想要打印出来dayOfMonth
而不是 x 的。我留给你的问题是如何使dayOfMonth
每次的价值增加。
回答by GGrec
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. 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.
这不是真正的编程问题,而是逻辑问题。如果你能连续集中注意力大约 4 分钟,你就会明白。但我想没有人会花时间做家庭作业了。坏程序员就是这样生出来的,请学会比开罐器更有野心。
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). Please take the time to review and improve this example. Everything is explained by comments.
代码没有优化。我只是像我想的那样离开了它(是的,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();
}
}