Java万年显示和查询_Java 万年历

import java.util.Scanner;

public class Demo {

//万年历

/**

* 编写一个万年历查询系统,可以查询公元元年至公元9999年间任一年的全年日历;

* 可以查询某年某月的月历,在屏幕上显示出来;

*/

/**

* 31 1 3 5 7 8 10 12

* 30 4 6 9 11

* 29 (闰年)

* 28 (平年)

* @param year

* @param month

* @return 返回具体月份在本年的具体天数

*/

public static int monthDays(int year,int month){

int days = 0;

switch (month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

days = 31;

break;

case 4:

case 6:

case 9:

case 11:

days = 30;

break;

case 2:

if(year%400==0 || (year%4==0 && year%100 != 0)){

days = 29;

}else{

days = 28;

}

break;

default:

days = 0;

System.out.println("输入的年月有误");

}

return days;

}

//计算今天是本年的第几天

public static int yearMonthDays(int year,int month,int day){

int days = 0;

for (int i = 1; i < month; i++) {

days += monthDays(year,i);

}

days = days+day;

return days;

}

//判断某年某月某日是星期几

public static int weeks(int year,int month,int day){

int days = yearMonthDays(year,month,day);

int week = (year-1+(year-1)/4-(year-1)/100+(year-1)/400+days)%7;

return week;

}

//打印具体某一月的日历

public static void printMonth(int year,int month){

int week = weeks(year,month,1);

int count = 0;

int days = monthDays(year,month);

System.out.println("日\t一\t二\t三\t四\t五\t六\t");

//打印空格

for (int i = 0; i < week; i++) {

System.out.print("\t");

count++;

}

//打印天

for (int i = 1; i <= days; i++) {

System.out.print(i+"\t");

count++;

if(count%7 == 0){

System.out.println();

}

}

}

//打印一年的日历

public static void printYears(int year){

for (int i = 1; i < 12; i++) {

printMonth(year,i);

System.out.println();

}

}

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

while(true){

System.out.println("功能选择:1.打印某年全年日历 2.打印某月日历 输入\"exit\"退出");

String f = reader.nextLine();

if("1".equals(f))

{

System.out.println("输入年份");

int year = reader.nextInt();

String s = reader.nextLine();//吃回车符

printYears(year);

} else if("2".equals(f)) {

System.out.println("输入yyyy-mm形式的年月");

String s3 = reader.nextLine();

String [] s4 = s3.split("\\-");

printMonth(Integer.parseInt(s4[0]),Integer.parseInt(s4[1]));

System.out.println();

}else if ("exit".equals(f)){

break;

}

}

}

}

标签:case,Java,万年历,int,days,month,year,public

来源: https://blog.csdn.net/qq_42929437/article/details/94893794

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写一个 Java 万年历程序。以下是代码示例: ```java import java.util.Scanner; public class Calendar { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入年份:"); int year = input.nextInt(); System.out.print("请输入月份:"); int month = input.nextInt(); // 计算这个月的天数 int days = getDaysOfMonth(year, month); // 计算这个月的第一天是星期几 int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印日历表头 System.out.println("日\t一\t二\t三\t四\t五\t六"); // 打印日历内容 int day = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDayOfWeek) { System.out.print("\t"); } else if (day > days) { break; } else { System.out.print(day + "\t"); day++; } } System.out.println(); } } // 获取指定年月的天数 public static int getDaysOfMonth(int year, int month) { int days = 0; switch (month) { case 2: if (isLeapYear(year)) { days = 29; } else { days = 28; } break; case 4: case 6: case 9: case 11: days = 30; break; default: days = 31; break; } return days; } // 判断指定年份是否是闰年 public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } // 获取指定年月的第一天是星期几 public static int getFirstDayOfWeek(int year, int month) { int century = year / 100; year = year % 100; int dayOfWeek = (1 + 5 * (year % 4) + 4 * (year % 100) + 6 * (year % 400)) % 7; if (century == 18) { dayOfWeek += 2; } else if (century == 20) { dayOfWeek += 6; } switch (month) { case 1: break; case 2: dayOfWeek += 3; break; case 3: dayOfWeek += 3; break; case 4: dayOfWeek += 6; break; case 5: dayOfWeek += 1; break; case 6: dayOfWeek += 4; break; case 7: dayOfWeek += 6; break; case 8: dayOfWeek += 2; break; case 9: dayOfWeek += 5; break; case 10: dayOfWeek += 0; break; case 11: dayOfWeek += 3; break; case 12: dayOfWeek += 5; break; } return dayOfWeek % 7; } } ``` 你可以直接运行这个程序,输入年份和月份,即可输出对应的万年历

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值