Вы находитесь на странице: 1из 3

ExCal.

java
import java.util.*;
class ExCal
{
public static void main(String[] args)
{
String month[] = {"J A N U A R Y", "F E B R U A R Y", "M A R C H", "A P R I L",
"M A Y", "J U N E", "J U L Y", "A U G U S T", "S E P T E M B E R",
"O C T O B E R", "N O V E M B E R", "D E C E M B E R"};
String wkday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Scanner in = new Scanner(System.in);


System.out.print("What year? (yyyy): ");
int y = in.nextInt();

int f, n;
f = 2 * (13) + (3 * (13+1)/5) + (y-1) + ((y-1)/4) - ((y-1)/100) + ((y-1)/400) + 2;
n = f % 7;
int counter=(n);

for(int dim=0, m=0; dim<daysInMonth.length && m<month.length; dim++, m++)


here:{
System.out.println("\n");
System.out.println(month[m]);
for(int w=0; w<wkday.length; w++)
System.out.print(wkday[w]+"\t");
System.out.println();

switch (counter)
{
case 1: System.out.print("\t");
break;
case 2: System.out.print("\t\t");
break;
case 3: System.out.print("\t\t\t");
break;
case 4: System.out.print("\t\t\t\t");
break;
case 5: System.out.print("\t\t\t\t\t");
break;
case 6: System.out.print("\t\t\t\t\t\t");
break;
}

for(int d=1; d<=daysInMonth[dim]; d++)


{
if(((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))
daysInMonth[1]=29;
else
daysInMonth[1]=28;
System.out.print(d + "\t");
counter++;
if(counter == 7)
{ System.out.println();
counter=0;
}
}
break here;
}
}
}

Page 1
ExCal.java
/*
This calendar program illustrates the basic concepts of java programming.
ARRAYS list the months, weekdays and days;
SCANNER inputs the year;
COUNTER starts New Years Day on the correct weekday;
NESTED for LOOPS instruct the program when to place the months, weekdays and days;
SWITCH starts the first of each month in the correct weekday;
if-else CONDITIONAL adjusts for Leap Year;
and BREAK instructs the program to go through each month.

OUTPUT

C:\>javac ExCal.java
C:\>java ExCal
What year? (yyyy): 2009

J A N U A R Y
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

F E B R U A R Y
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

M A R C H
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
A P R I L
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
M A Y
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
J U N E
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

Page 2
ExCal.java
J U L Y
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
A U G U S T
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
S E P T E M B E R
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
O C T O B E R
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

N O V E M B E R
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
D E C E M B E R
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

C:\>
*/

Page 3