统计信息
- 访问数:12256
- 博客数:142
- 建立时间:2008-01-05
- 更新时间:2008-05-21
P4Q13
2008-02-15 17:31:55
天气: 晴朗 心情: 高兴
/*P4Q13
* Name:Wang Chaoran
* Description:13 (Displaying calendars)
Write a program that prompts the user to enter the year and first day of the year,
and displays the calendar table for the year on the console. For example,
if the user entered the year 2005, and 6 for Saturday, January 1, 2005,
your program should display the calendar for each month in the year, as follows:
January 2005
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
...
December 2005
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
*/
import java.util.Scanner;
public class P4Q13
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the year(eg.2005):");
int year = scan.nextInt();
int leapDay = 0,sum = 0,monthDay= 0;
System.out.println("Please enter the first day of the year(eg.6 for Saturday)");
sum = scan.nextInt();
String dayString ="",monthString="";
//judgement of leap year
if(((year%4==0)&&(year%100!=0))||(year%400==0))
leapDay=29;
else
leapDay=28;
System.out.println("PLease enter the first day of the year(eg.6 for Saturday)");
for(int month=1;month<=12;month++)
{
//Obtain the dayString
switch(sum%7)
{
case 1:dayString = " ";
break;
case 2:dayString = " ";
break;
case 3:dayString = " ";
break;
case 4:dayString = " ";
break;
case 5:dayString = " ";
break;
case 6:dayString = " ";
break;
case 0:dayString = "";
}
//Obtain the monthString + facilitate the dayString
switch(month)
{
case 1: sum+=31;monthDay=31;monthString="January";
break;
case 2: sum+=leapDay;monthDay=leapDay;monthString="February";
break;
case 3:sum+=31;monthDay=31;monthString="March";
break;
case 4:sum+=30;monthDay=30;monthString="April";
break;
case 5:sum+=31;monthDay=31;monthString="May";
break;
case 6:sum+=30;monthDay=30;monthString="June";
break;
case 7:sum+=31;monthDay=31;monthString="July";
break;
case 8:sum+=31;monthDay=31;monthString="August";
break;
case 9:sum+=30;monthDay=30;monthString="September";
break;
case 10:sum+=31;monthDay=31;monthString="October";
break;
case 11:sum+=30;monthDay=30;monthString="November";
break;
case 12:sum+=31;monthDay=31;monthString="December";
}
//Display the first two lines
System.out.println("\n\n"+monthString+" "+year);
System.out.println("Sun Mon Tue Wed Thu Fri Sat ");
//Display the rest of the lines
int count = 0;
for(int i=1;i<=monthDay;i++)
{
count++;
if(i==1)
{
System.out.print(dayString);
}
if(((sum-monthDay)%7+count)%7==0)
System.out.printf("%-4s\n",i);
else
System.out.printf("%-4s",i);
}
}
}
}
for(int i=0;i=sum%7;i++)
{
System.out.printf("%4s","");
}
导入论坛查看(77)回复(0)引用(0)好评(0) 差评(0)
加入收藏 编辑 审核TAG: computing