简介:适合初学者的一个日历,尽可能避免导入Java的包。
要求:
- 1.输入1970之后的任意年月,在控制台打印出指定年月的所有天数,并且与第一行的“日、一、二、三、四、五、六”对齐,还需要保证准确性。
- 2.星期日这一列红色显示。
- 3.将控制台输出的内容全部保存到一个txt文件中,保存之后打开该文件,看到的就是一个和控制台一样格式整齐的日历。
- 4.尽量不要用Java自带的获取指定天数是星期几的方法,通过编写算法来锻炼一下逻辑思维能力
实现
工具:win7操作系统的电脑,eclipse
代码:
package com.qst.hgl.mycalendar;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Calendar {
private static int year, month;// 年月日
private static int leapyear_flag; // 闰年为1,平年为0,为2月设计
private static int alldays; // 一个月有多少天
private static int weekday; // 指定年月的1号是星期几0~6对应星期日至星期一至星期六
private static int yearnumbers = 1;// 1970年到输入年份之间,有多少个闰年
private static int monthnumbers;// 从当年1月1号到输入月份1号之间,有多少个31号
private static int num;// 从1970年1月1日到输入年月1号过了几天
private static int changeline;// 换行记位数
public static void main(String[] args) throws InterruptedException {
// 分别 获取输入的年和月
System.out.println("请输入年份:\n");
Scanner sc1 = new Scanner(System.in);
year = sc1.nextInt();
// 判断年份数值是否越界(必须是1970之后的年份)
if (year < 1970) {
System.out.println("请输入1970年之后的年份:\n");
Scanner sc11 = new Scanner(System.in);
year = sc11.nextInt();
System.out.println("请输入月份:\n");
} else {
System.out.println("请输入月份:\n");
}
Scanner sc2 = new Scanner(System.in);
month = sc2.nextInt();
Calendar.yeartype(year);
Calendar.monthdaynumbers(month);
Calendar.daynumbers(year, month);
Calendar.showcalendar();
Calendar.save(year, month);
}
// 第一步, 判断是否闰年,给闰年做个标记leapyear_flag,求出1970年到输入年份有几个闰年,方便下一步月份日期判断
public static void yeartype(int year) {
for (int i = 1970; i < year; i++) {
if (i % 400 == 0 || i % 4 == 0 && i % 100 != 0) {
leapyear_flag = 1;
yearnumbers = yearnumbers + 1;
} else {
leapyear_flag = 0;
}
}
}
// 第二步,判断不同年的不同月有alldays天,特别是二月,并记录中间有monthnumbers个31天的月份
public static void monthdaynumbers(int month) {
switch (month) {
case 1:
alldays = 31;
monthnumbers = 0;
break;
case 2:
alldays = 28 + leapyear_flag;
monthnumbers = 1;
break;
case 3:
alldays = 31;
monthnumbers = 1;
break;
case 4:
alldays = 30;
monthnumbers = 2;
break;
case 5:
alldays = 31;
monthnumbers = 2;
break;
case 6:
alldays = 30;
monthnumbers = 3;
break;
case 7:
alldays = 31;
monthnumbers = 3;
break;
case 8:
alldays = 31;
monthnumbers = 4;
break;
case 9:
alldays = 30;
monthnumbers = 5;
break;
case 10:
alldays = 31;
monthnumbers = 5;
break;
case 11:
alldays = 30;
monthnumbers = 6;
break;
case 12:
alldays = 31;
monthnumbers = 6;
break;
}
}
// 判断1970年1月1日到输入年月的第一天过了num天
public static void daynumbers(int year, int month) {
int twomonth;
if (month > 2) {
twomonth = 2;// 输入月份是否比2月大
} else {
twomonth = 0;
}
// 把闰年当平年,理想的算出1970年1月1日到指定年1月1日有(year - 1970) * 365天,
// 再加上闰年数yearnumbers就是实际的天数,
// 再把每个月都当30天处理,加上指定年1月1号到指定月1号过了30 * (month - 1)天,
// 减去2月份亏欠的天数twomonth,加上31天月份未加的部分 monthnumbers
num = (year - 1970) * 365 + yearnumbers + 30 * (month - 1)
+ monthnumbers - twomonth;
//顺便还可以求出当前月份1号是星期weekday,因为1970年1月1号是星期4
weekday = (num + 3) % 7;
}
// 打印方法,判断什么时候换行,第一行该打印出几天,以及第一天放哪里,1970年1月1日是星期四
public static void showcalendar() throws InterruptedException {
System.out.println("日\t一\t二\t三\t四\t五\t六\n");
// 判断从1号开始,几天后开始换行
switch (weekday) {
case 0:// 星期天
changeline = 6;
break;
case 1:// 星期一
changeline = 5;
break;
case 2:// 星期二
changeline = 4;
break;
case 3:// 星期三
changeline = 3;
break;
case 4:// 星期四
changeline = 2;
break;
case 5:// 星期五
changeline = 1;
break;
case 6:// 星期六
changeline = 0;
break;
}
//不显示上个月的日期,用空格代替
for (int i = 0; i < weekday; i++) {
System.out.print(" \t");
}
// 开始打印,并准确换行
for (int k = 1; k < alldays + 1; k++) {
if ((k + weekday) % 7 == 1)// 每行第一个数的处理
{
Thread.sleep(10);// 设置取数间隔时间,单位ms
System.err.flush();// 清除缓存区,这样就不会错位
System.err.print(k + "\t");//用红色字体打印每行第一个日期
} else if ((k + weekday) % 7 == 0) {
Thread.sleep(10);
System.out.flush();
System.out.println(k + "\t");// 每行末尾日期输完之后要换行
} else {
Thread.sleep(10);
System.out.flush();
System.out.print(k + "\t");//打印其他日期
}
}
}
// 保存到文本文档
public static void save(int year, int month) {
try {
String yearandmonth = "您输入的是:" + year + "年" + month + "月" + "\n\n\n";
String weekendaywords = "日\t一\t二\t三\t四\t五\t六\n";
// 先把每个数包括各种符号按顺序装入数组
File file = new File("save.txt");
if (!file.exists()) {
file.createNewFile();
}
String arrays[] = new String[40];
for (int i = 0; i < weekday; i++) {
arrays[i] = "\t";
}
for (int k = 1; k < alldays + 1; k++) {
if ((k + weekday) % 7 == 1)// 每行第一个数的处理
{
arrays[k - 1 + weekday] = k + "\t";
} else if ((k + weekday) % 7 == 0) {
arrays[k - 1 + weekday] = k + "\t\n";
} else {
arrays[k - 1 + weekday] = k + "\t";
}
}
// 把打包完后的数据逐个以追加方式读入txt
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);//true代表追加
BufferedWriter bw = new BufferedWriter(fw);
bw.write(yearandmonth);
bw.write(weekendaywords);
for (int g = 1; g < alldays + 1 + weekday; g++) {
bw.write(arrays[g - 1]);
}
bw.write("\n\n\n------------------------------------\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行截图:
本程序简单至极,copy到eclipse就可以运行,无需其他操作。
如果对你有帮助,可以给我点个赞。
做程序员最快乐的事,就是自己码的代码被别人认可了。