演示题目:编写程序,输入年份,打印出该年的年历(12个月的)。
首先我们先看看效果:
这便是我们想要达到的效果,那么要怎么完成呢,请听我慢慢道来。
首先,我们需要一个可以输入年份的代码,这也很简单,用Scanner类可以做到。
Scanner sc = new Scanner(System.in);
System.out.println("Please input years:");
int year = sc.nextInt();
这便是简单的输入年份,但是光有年份是远远不够的,我们还得把整个日历给设计出来。
这里我们引入一个类Calendar,这个是java里自带的类,调用
import java.util.Calendar;即可。
老规矩,我们先用Calendar类开辟一个内存,方便之后使用其方法,我将它取名为c。然后我们需要了解Calendar中所包含的方法,在java文档中里面有详细的记载,网址献上:在线文档-jdk-zh,然后我们选择我们需要的方法,很明显,我们需要知道每年的第一天是星期几,每个月的第一天是星期几,那么我们就需要set一个日期,输入我们想要的年份月份即可知道第一天对应星期几了,那我们就需要以下几种方法:
System.out.println("Please input years:");
int month=i;
int date=1;
c.set(year,month,date);
c.set(Calendar.YEAR, year);
int x = c.get(Calendar.DAY_OF_WEEK);
c.set(Calendar.MONTH, month);
int y = c.getActualMaximum(Calendar.DAY_OF_MONTH);
其中y就是每个月份中天数的最大值,即每个月的天数。x就是每年第一天和每个月第一天所对应的星期几,要注意的是,星期天对应的是1,星期六对应的是7。
完事后,我们开始构思具体代码该怎么写,首先可以从最简单的入手,那就是框架,这里我在for循环里加了一个switch语句,用来搭建代码框架:
for (int i = 0; i < 12; i++) {
switch (i + 1) {
case 1:
String a0 = "January";
System.out.println("Month's name is " + (a0));
break;
case 2:
String a1 = "February";
System.out.println("Month's name is " + (a1));
break;
case 3:
String a2 = "March";
System.out.println("Month's name is " + (a2));
break;
case 4:
String a3 = "April";
System.out.println("Month's name is " + (a3));
break;
case 5:
String a4 = "May";
System.out.println("Month's name is " + (a4));
break;
case 6:
String a5 = "June";
System.out.println("Month's name is " + (a5));
break;
case 7:
String a6 = "July";
System.out.println("Month's name is " + (a6));
break;
case 8:
String a7 = "August";
System.out.println("Month's name is " + (a7));
break;
case 9:
String a8 = "September";
System.out.println("Month's name is " + (a8));
break;
case 10:
String a9 = "October";
System.out.println("Month's name is " + (a9));
break;
case 11:
String a10 = "November";
System.out.println("Month's name is " + (a10));
break;
case 12:
String a11 = "December";
System.out.println("Month's name is " + (a11));
break;
}//复杂了,但是我懒得改了。
System.out.println("==================================");
System.out.println("SUN" + " " + "MON" + " " + "THU" + " " + "WED" + " " + "THE" + " " + "FRI" + " " + "SAT");
}
这个就是基本框架,然后我们要做的就是往里面填数字,首先我们要知道,每个月份的第一天是和其对应的星期几对应的,我们又已经确定了每月的第一天是星期几,那我们就可以用for循环来实现第一天在哪里开始:
for (int j = 0; j < (y+x-1); j++) {
if (j < (x-1)) {
System.out.print(" ");
}
确定好了从那开始之后,我们需要一个计数器从0开始往下加,但是我们要注意,每七天要换一行,所以我们可以用这样的方式来实现:
else{
day++;
System.out.printf("%-5s",day);
if((x+day-1)%7==0)
{
System.out.print("\n");
}
这样我们就可以把每一月份的天数全部打印出来,其中“%d-5s”是左对齐的意思,其只能用println函数完成。
完整代码如下:
import java.util.Calendar;
import java.util.Scanner;
public class WriteCalendar {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
System.out.println("Please input years:");
int year = sc.nextInt();
for (int i = 0; i < 12; i++) {
int month=i;
int date=1;
c.set(year,month,date);
c.set(Calendar.YEAR, year);
int x = c.get(Calendar.DAY_OF_WEEK);
c.set(Calendar.MONTH, month);
int y = c.getActualMaximum(Calendar.DAY_OF_MONTH);
switch (i + 1) {
case 1:
String a0 = "January";
System.out.println("Month's name is " + (a0));
break;
case 2:
String a1 = "February";
System.out.println("Month's name is " + (a1));
break;
case 3:
String a2 = "March";
System.out.println("Month's name is " + (a2));
break;
case 4:
String a3 = "April";
System.out.println("Month's name is " + (a3));
break;
case 5:
String a4 = "May";
System.out.println("Month's name is " + (a4));
break;
case 6:
String a5 = "June";
System.out.println("Month's name is " + (a5));
break;
case 7:
String a6 = "July";
System.out.println("Month's name is " + (a6));
break;
case 8:
String a7 = "August";
System.out.println("Month's name is " + (a7));
break;
case 9:
String a8 = "September";
System.out.println("Month's name is " + (a8));
break;
case 10:
String a9 = "October";
System.out.println("Month's name is " + (a9));
break;
case 11:
String a10 = "November";
System.out.println("Month's name is " + (a10));
break;
case 12:
String a11 = "December";
System.out.println("Month's name is " + (a11));
break;
}//复杂了,但是我懒得改了。
System.out.println("==================================");
System.out.println("SUN" + " " + "MON" + " " + "THU" + " " + "WED" + " " + "THE" + " " + "FRI" + " " + "SAT");
int day = 0;
for (int j = 0; j < (y+x-1); j++) {
if (j < (x-1)) {
System.out.print(" ");
}
else{
day++;
System.out.printf("%-5s",day);
if((x+day-1)%7==0)
{
System.out.print("\n");
}
}
}
System.out.println("\n");
}
}
}
感谢大家观看,希望对大家有所帮助!