To start, I should state that I'm a first year Computer Science student so there's a high chance I've done something stupid
Anywho,
I've got an assignment to display a calendar given an input of month and year in Java.
The output is supposed to look like this
[code] March 2012
Sun Mon Tue Wed Thu Fri Sat
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[/code]
I've got the code to calculate the number of days in a month. Making it display the month, year and days is easy, and I've also got it properly formatted so it starts numbering at the correct first day of the month.
But I haven't got the slightest clue how to format the rest of the numbers so they correctly fall in line within the calendar :v:
My current code for the output is this
[php] public static void displayMonthlyCalendar (int year, int month)
{
String monthName, spacing;
int days, ctr = 2;
monthName = monthName (month);
System.out.println ("\t " + monthName + " " + year);
System.out.println ("\nSun Mon Tue Wed Thu Fri Sat");
spacing = determineFirstDaySpacing (year, month);
System.out.print (spacing);
days = determineNumberOfDaysInMonth (year, month);
while (ctr <= days)
{
System.out.print (" " + ctr);
ctr++;
}
}//end displayMonthlyCalendar()[/php]
But that doesn't exactly work, gives me output like this
[code] March 2012
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7 8 9 10 etc.[/code]
Help?
You could run "cal <month> <year>" to get a calendar externally.
[QUOTE=Jookia;33216266]You could run "cal <month> <year>" to get a calendar externally.[/QUOTE]
I know, but our teacher isn't going to let us do that.
[QUOTE=Bazkip;33216290]I know, but our teacher isn't going to let us do that.[/QUOTE]
If the assignment is to [I]display[/I] a calender, why not think outside the box?
Anyway, keep a variable of which position you're up to horizontally, so in your example it'd be Thursday, which is position 4. Once the position reaches 8, set it to 1 and go down a line.
[QUOTE=Jookia;33216357]If the assignment is to [I]display[/I] a calender, why not think outside the box?
Anyway, keep a variable of which position you're up to horizontally, so in your example it'd be Thursday, which is position 4. Once the position reaches 8, set it to 1 and go down a line.[/QUOTE]
There's more to the assignment than just display the calendar, there's specific things we need to do. And I'm almost certain my teacher would outright fail me if I did that.
As for the second part, I have no idea what that means :v:
Mentally label the columns 0-7. Start a counter at the appropriate column (so, 4 for Thursday), then increment each time you add a new number. When you go over 7, insert a line break and reset the counter to 0. That way you'll start at the first column.
[QUOTE=Jookia;33216478]Mentally label the columns 0-7. Start a counter at the appropriate column (so, 4 for Thursday), then increment each time you add a new number. When you go over 7, insert a line break and reset the counter to 0. That way you'll start at the first column.[/QUOTE]
[img]http://dl.dropbox.com/u/35476992/Monthcalendar.png[/img]
VICTORY! :dance:
Sorry, you need to Log In to post a reply to this thread.