欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java学习day017  Java显示日历程序

程序员文章站 2024-03-16 18:46:04
...

day017  Java显示日历程序

Java自顶向下的程序设计方法需要前期分析清楚程序的结构与功能。


Method Abstraction and Stepwise Refinement

Suppose you write a program that displays the calendar for a given month of the year. The program prompts the user to enter the year and the month, then displays the entire calendar for the month, as shown in the following sample run:

        Java学习day017  Java显示日历程序

Top-Down Design How would you get started on such a program? Would you immediately start coding? Beginning programmers often start by trying to work out the solution to every detail. Although details are important in the final program, concern for detail in the early stages may block the problem-solving process. To make problem solving flow as smoothly as possible, this example begins by using method abstraction to isolate details from design and only later implements the details.

For this example, the problem is first broken into two subproblems: get input from the user, and print the calendar for the month. At this stage, you should be concerned with what the subproblems will achieve, not with how to get input and print the calendar for the month. You can draw a structure chart to help visualize the decomposition of the problem

   Java学习day017  Java显示日历程序

The structure chart shows that the printCalendar problem is divided into two subproblems, readInput and printMonth, and that printMonth is divided into two smaller subproblems, printMonthTitle and printMonthBody.

The problem of printing the calendar for a given month can be broken into two subproblems: print the month title, and print the month body, as shown in Figure 5.9(b). The month title consists of three lines: month and year, a dash line, and the names of the seven days of the week. You need to get the month name (e.g., January) from the numeric month (e.g., 1). This is accomplished in getMonthName

      Java学习day017  Java显示日历程序

In order to print the month body, you need to know which day of the week is the first day of the month (getStartDay) and how many days the month has getNumberOfDaysInMonth), as shown in Figure 5.10(b). For example, December 2005 has 31 days, and December 1, 2005, is Thursday. How would you get the start day for the first date in a month? There are several ways to do so. For now, we’ll use the following approach. Assume you know that the start day (startDay1800 = 3) for Jan 1,1800, was Wednesday. You could compute the total number of

   Java学习day017  Java显示日历程序

days (totalNumberOfDays) between Jan 1, 1800, and the first date of the calendar month. The start day for the calendar month is (totalNumberOfDays + startDay1800) % 7, since every week has seven days. So the getStartDay problem can be further refined as getTotalNumberOfDays, as shown in Figure 5.11(a). To get the total number of days, you need to know whether the year is a leap year and the number of days in each month. So getTotalNumberOfDays is further refined into two subproblems: isLeapYear and getNumberOfDaysInMonth, as shown in Figure 5.11(b).

 


方法抽象和逐步细化

假设您编写了一个程序来显示给定月份的日历。程序提示用户输入年份和月份,然后显示该月的整个日历,如下面的示例运行所示:

Java学习day017  Java显示日历程序

自顶向下的设计如何开始这样一个项目?你能马上开始编码吗?刚开始编程的人常常试图解决每一个细节问题。虽然细节在最终项目中很重要,但是在早期阶段对细节的关注可能会阻碍问题解决过程。为了使问题解决流程尽可能顺畅,本例首先使用方法抽象将细节与设计隔离开来,然后才实现细节。对于本例,首先将问题分解为两个子问题:从用户获取输入,并打印当月的日历。在此阶段,您应该关注子问题将实现什么,而不是如何获取输入和打印当月的日历。您可以画一个结构图来帮助可视化问题的分解

Java学习day017  Java显示日历程序

结构图显示,printCalendar问题被划分为两个子问题readInput和printMonth,而printMonth又被划分为两个更小的子问题printMonthTitle和printMonthBody。给定月份的日历打印问题可以分解为两个子问题:打印月份标题,打印月份正文,如图5.9(b)所示。月的标题由三行组成:月和年,虚线,一周七天的名称。您需要从数字月份(例如,1)中获取月份名称(例如,1),这是在getMonthName中完成的

Java学习day017  Java显示日历程序

为了打印这个月的主体,您需要知道一周中的哪一天是这个月的第一天(getStartDay),以及这个月有多少天(getNumberOfDaysInMonth),如图5.10(b)所示。例如,2005年12月有31天,而2005年12月1日是星期四。你如何确定一个月中的第一次约会的开始日期?有几种方法可以做到这一点。现在,我们将使用以下方法。假设您知道1800年1月1日的开始日期(startDay1800 = 3)是星期三。你可以计算它们的总数

Java学习day017  Java显示日历程序

1800年1月1日至日历月第一天。日历月的起始日为(totalNumberOfDays + startDay1800) % 7,因为每个星期有7天。因此,getStartDay问题可以进一步细化为getTotalNumberOfDays,如图5.11(a)所示。为了得到总天数,你需要知道年份是否为闰年以及每个月的天数。因此,getTotalNumberOfDays进一步细化为两个子问题:isLeapYear和getNumberOfDaysInMonth,如图5.11(b)所示。

以上是这个程序的中文英文分析,自顶向下的编程思想方法。

下面是这个显示日历的程序


/*
 *@zzhao
*/


package HelloJava;

import java.util.Scanner;

public class ShowTime
{
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a year :");
		int year=input.nextInt();
		System.out.print("Enter the first day of the year is a few weeks :");
		int week=input.nextInt();
		String M=null;
		System.out.print("Enter a mouth :");
		int mouth=input.nextInt();
		int W;
		int count=0;
		boolean LeapYear=(year%4==0&&year%100!=0)||(year%400==0);
	
			if(mouth==1)
			{
				M="January";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++)
				{
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++)
				{

					if(i%7==0)
					{
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++)
				{
					count++;
					if(count%7==0)
					{
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}


			else if(mouth==2)
			{
				W=(31%7+week)%7;
				week=W;
				M="February";
				if(LeapYear)
				{
					System.out.println("\t\t  "+M+"\t"+year);
					System.out.println("----------------------------------------------------");
					System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
					for(int j=0;j<week;j++)
					{
						System.out.print("\t");
					}
					for(int i=1;i<=7-week;i++)
					{

						if(i%7==0)
						{
							System.out.println(i);
						}else
							System.out.print(i+"\t");
					}
					System.out.println();
					for(int k=(7-week+1);k<=29;k++)
					{
						count++;
						if(count%7==0)
						{
							System.out.println(k);
						}else
							System.out.print(k+"\t");
					}
					System.out.println();
					count=0;
				}
				else
				{
					System.out.println("\t\t  "+M+"\t"+year);
					System.out.println("----------------------------------------------------");
					System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
					for(int j=0;j<week;j++)
					{
						System.out.print("\t");
					}
					for(int i=1;i<=7-week;i++)
					{
						if(i%7==0)
						{
							System.out.println(i);
						}else
							System.out.print(i+"\t");
					}
					System.out.println();
					for(int k=(7-week+1);k<=28;k++)
					{
						count++;
						if(count%7==0)
						{
							System.out.println(k);
						}else
							System.out.print(k+"\t");
					}
					System.out.println();
					count=0;
				}
			}
			else if(mouth==3)
			{
				if(LeapYear)
				{
					W=(29%7+week)%7;
					week=W;
					M="March";
					System.out.println("\t\t  "+M+"\t"+year);
					System.out.println("----------------------------------------------------");
					System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
					for(int j=0;j<week;j++)
					{
						System.out.print("\t");
					}
					for(int i=1;i<=7-week;i++)
					{
						if(i%7==0)
						{
							System.out.println(i);
						}else
							System.out.print(i+"\t");
					}
					System.out.println();
					for(int k=(7-week+1);k<=31;k++)
					{
						count++;
						if(count%7==0)
						{
							System.out.println(k);
						}else
							System.out.print(k+"\t");
					}
					System.out.println();
					count=0;
				}
				else{W=(28%7+week)%7;
				week=W;
				M="March";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++)
				{
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++)
				{
					if(i%7==0)
					{
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++)
				{
					count++;
					if(count%7==0)
					{
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
				}
			}

			else if(mouth==4)
			{
				W=(31%7+week)%7;
				week=W;
				M="April";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=30;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==5){
				W=(30%7+week)%7;
				week=W;
				M="May";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==6){
				W=(31%7+week)%7;
				week=W;
				M="June";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=30;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==7){
				W=(30%7+week)%7;
				week=W;
				M="July";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==8){
				W=(31%7+week)%7;
				week=W;
				M="August";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==9){
				W=(31%7+week)%7;
				week=W;
				M="September";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=30;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==10){
				W=(30%7+week)%7;
				week=W;
				M="October";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==11){
				W=(31%7+week)%7;
				week=W;
				M="November";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=30;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();
				count=0;
			}else if(mouth==12){
				W=(30%7+week)%7;
				week=W;
				M="December";
				System.out.println("\t\t  "+M+"\t"+year);
				System.out.println("----------------------------------------------------");
				System.out.println("Sun"+"\t"+"Mon"+"\t"+"Tue"+"\t"+"Wed"+"\t"+"Thu"+"\t"+"Fri"+"\t"+"Sat");
				for(int j=0;j<week;j++){
					System.out.print("\t");
				}
				for(int i=1;i<=7-week;i++){

					if(i%7==0){
						System.out.println(i);
					}else
						System.out.print(i+"\t");
				}
				System.out.println();
				for(int k=(7-week+1);k<=31;k++){
					count++;
					if(count%7==0){
						System.out.println(k);
					}else
						System.out.print(k+"\t");
				}
				System.out.println();count=0;
			}

		

	}
}       

运行的结果如下所示:

Java学习day017  Java显示日历程序


如果你想直接通过输入一年之后可以显示整年的日历,你可以使用for循环将if else if包起来,从1月到12月进行循环输出,这样就可以直接得到某一年的完整日历了。