一个简单的万年历制作
简单介绍万年历的各功能实现:
1.首先键盘输入查询的年份和月份
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt();
System.out.println("请输入月份");
int month = sc.nextInt();
//判断年月是否输入正确
while(year<1900)
{
System.out.println("你输入的年份不正确,请重新输入年份");
year = sc.nextInt();
}
while(month>12||month<=0)
{
System.out.println("你输入的月份不正确,请重新输入月份");
month = sc.nextInt();
}
2.然后对该年份的属性进行判断(平年or闰年):
//判断该年是闰年还是平年
public static boolean YearType(int year)
{
if ((year%4==0 &&year%100!=0)||year%400==0)
{
return true;
}else{
return false;
}
}
3.对输入年份每个月的天数进行判断
//判断该年每个月的天数
public static int day(int month,int year)
{
if(month==4||month==6||month==9||month==11){
return 30;
}else if(month==2){
if(YearType(year)){
return 29;
}else{
return 28;
}
}else{
return 31;
}
}
4.打印日历表
public static void WeekTable(int month,int year,int dayall,int monthday){
//
System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t");
for(int i=0;i<=day(month,year)+(dayall+monthday)%7;i++)
{
if(i<=(dayall+monthday)%7){
System.out.print("\t");
}else{
System.out.print((i-(dayall+monthday)%7)+"\t");
}
if((i+1)%7==0&&i!=0){
System.out.println();
}
}
}
5.对以上进行整合,最后附上源代码:
import java.util.Scanner;
class Calendar
{
public static void main(String[] args)
{
System.out.println("欢迎进入万历表查询系统");
System.out.println("---------------------------------------------------");
boolean button=true;
while(button!=false){
Input();
System.out.println("输入'true'继续查询,'false'退出系统!");
Scanner sc = new Scanner(System.in);
button=sc.nextBoolean();
if(!button){
System.out.println("谢谢你的使用,再见!");
}
}
}
public static void Input(){
int dayall=0,monthday=0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt();
while(year<1900)
{
System.out.println("你输入的年份不正确,请重新输入年份");
year = sc.nextInt();
}
System.out.println("请输入月份");
int month = sc.nextInt();
while(month>12||month<=0)
{
System.out.println("你输入的月份不正确,请重新输入月份");
month = sc.nextInt();
}
for (int i=1900+1;i<=year;i++)
{
if(YearType(i)){
dayall+=366;
//System.out.println("闰年");
}else{
dayall+=365;
//System.out.println("平年");
}
}
for (int i=1;i