Code
import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
int[] daysInMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] months = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Scanner userIn = new Scanner(System.in);
System.out.println("Please enter the first day of the year (1 for Monday, etc)");
int userDay = userIn.nextInt();
System.out.println("Please enter what year it is");
int userYear = userIn.nextInt();
printCalendar(userDay, userYear, months, daysInMonth);
if (isLeapYear(userYear)) {
System.out.println(userYear + " Was a leap year!");
}
}
public static boolean isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
public static void printCalendar(int day, int year, String[] months, int[] daysInMonth) {
int startDay;
for (int i = 0; i < months.length; i++) {
System.out.println("\t" + months[i] + " " + year);
System.out.println("____________________________");
System.out.println(" " + "Sun" + " " + "Mon" + " " + "Tue" + " " + "Wed" + " " + "Thu" + " " + "Fri" + " " + "Sat");
switch (day) {
case 1: System.out.println(" " + "1" + " " + "2" + " " + "3" + " " + "4" + " " + "5" + " " + "6" + "\n");
startDay = 7;
break;
case 2: System.out.println(" " + "1" + " " + "2" + " " + "3" + " " + "4" + " " + "5" + "\n");
startDay = 6;
break;
case 3: System.out.println(" " + "1" + " " + "2" + " " + "3" + " " + "4" + "\n");
startDay = 5;
break;
case 4: System.out.println(" " + "1" + " " + "2" + " " + "3" + "\n");
startDay = 4;
break;
case 5: System.out.println(" " + "1" + " " + "2" + "\n");
startDay = 3;
break;
case 6: System.out.println(" " + "1" + "\n");
startDay = 1;
break;
default: System.out.println(" " + "1" + " " + "2" + " " + "3" + " " + "4" + " " + "5" + " " + "6" + " " + "7" + "\n");
startDay = 8;
break;
}
int k = daysInMonth[i];
if (i == 1 && isLeapYear(year)) {
k = 29;
}
int count = 0;
int currentDay = 0;
for (int s = startDay; s <= k; s++) {
System.out.printf("%4d", s);
count++;
currentDay++;
if (count % 7 == 0) {
System.out.println("\r");
}
if (currentDay == 7 && s != k-1) {
currentDay = 0;
}
}
day = currentDay;
System.out.println("\n");
}
}
}
one of the first projects I did. Easy stuff, we are just getting our feet wet w/ java syntax a little bit.
Took me longer than I wanted because I am still a horrible programmer.
if you find stupid and/or unnecessary shit, don't be surprised.
I hope I get better as time goes on. I don't like getting stuck on easy shit like this :/
This post was edited by Eep on Feb 13 2013 09:18pm