Java實(shí)現(xiàn)簡單的萬年歷
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡單萬年歷的具體代碼,供大家參考,具體內(nèi)容如下
1 要求
1、輸入年份;
2、輸入月份;
3、輸出某年某月的日歷。
2 思路
1、實(shí)現(xiàn)從控制臺接收年和月,判斷是否是閏年(判斷是否是閏年:能被4整除但不能被100整除;或者能被400整除);
2、計算輸入月份的天數(shù);
3、計算該月第一天是星期幾;
3.1 計算輸入年份距離1900年1月1日的天數(shù);
3.2 計算輸入月份之前的天數(shù)(從當(dāng)年年初開始);
3.3 將以上兩組數(shù)據(jù)進(jìn)行求和;
3.4 已知該月之前的天數(shù),計算輸入月份的第一天是星期幾(從1900年1月1日(星期一)開始推算: 星期幾 = 1 + 天數(shù)差 % 7 )。
4、按格式輸出該月日歷 。
3 源代碼
import java.util.Scanner;
public class index {
//每個月的天數(shù)
public static int monthday(int month, int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
int[] day = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return day[month];
} else {
int[] day = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return day[month];
}
}
//月份總天數(shù)
public static int monthdays(int month, int year) {
int totaldays = 0;
for (int i = 1; i < month; i++) {
totaldays = totaldays + monthday(i, year);
}
return totaldays;
}
//距離1900年的年份總天數(shù)
public static int yeardays(int year){
int yeardays = 0;
for (int i = 1900;i<year;i++){
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
yeardays = yeardays+366;
} else {
yeardays = yeardays+365;
}
}
System.out.println(year+"年距離1900年的總天數(shù)"+yeardays);
return yeardays;
}
//輸出日歷
public static void printCalendar(int month,int year){
int totaldays = 0;
if (year > 0) {
if (month > 0 && month < 13) {
//距離1900年1月1日總天數(shù)
totaldays = monthdays(month,year)+yeardays(year);
System.out.println(year+"年"+month+"月1日距離1900年的總天數(shù):"+totaldays);
System.out.println("\n**********"+year+"年"+month+"月的日歷為**********");
System.out.println("一\t二\t三\t四\t五\t六\t日\t");
int week = 1+totaldays%7;
//根據(jù)1日為周幾輸出空格
for(int i=1;i<week;i++){
System.out.print(" \t");
}
//輸入具體日期
for(int i=1;i<=monthday(month,year);i++){
System.out.print(i+"\t");
if(week==7){
week = 1;//重置為星期一
System.out.println();
}else{
week++;
}
}
} else {
System.out.println("輸入的月份不合法!");
}
} else {
System.out.println("輸入的年份不合法!");
}
}
//主函數(shù)
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("***********************歡迎使用萬年歷**************************");
System.out.println("*********請選擇你需要進(jìn)行的操作(輸入進(jìn)行操作之前的數(shù)字)**********");
System.out.println("********************1.查詢某年某月的日歷************************");
System.out.println("********************2.結(jié)束操作*********************************");
System.out.print("\n請選擇你需要進(jìn)行的操作:");
int a = scanner.nextInt();
for (int i=0;i>=0;i++) {
switch (a) {
case 1:
System.out.print("請選擇年份:");
int year = scanner.nextInt();
System.out.print("請選擇月份:");
int month = scanner.nextInt();
printCalendar(month, year);
System.out.print("\n請選擇你需要進(jìn)行的操作:");
a = scanner.nextInt();
break;
case 2:
System.out.println("退出程序成功!");
return;
}
}
}
}
4 結(jié)果截圖

注意:我的周日是在最后一欄
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java正則表達(dá)式學(xué)習(xí)筆記之命名捕獲
這篇文章主要為大家詳細(xì)介紹了java正則表達(dá)式中的命名捕獲,感興趣的小伙伴們可以參考一下2016-02-02
Java中shiro框架和security框架的區(qū)別
這篇文章主要介紹了Java中shiro框架和security框架的區(qū)別,shiro和security作為兩款流行的功能強(qiáng)大的且易于使用的java安全認(rèn)證框架,在近些年中的項目開發(fā)過程中使用廣泛,今天我們就來一起了解一下兩者的區(qū)別2023-08-08
異常點(diǎn)/離群點(diǎn)檢測算法——LOF解析
這篇文章主要介紹了異常點(diǎn)/離群點(diǎn)檢測算法——LOF解析,通過圖解文字描述的方式詳細(xì)的解析了該算法,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
SpringBoot+hutool實(shí)現(xiàn)圖片驗證碼
本文主要介紹了SpringBoot+hutool實(shí)現(xiàn)圖片驗證碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Java實(shí)現(xiàn)數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)數(shù)組的方法分析
這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)數(shù)組的方法,結(jié)合實(shí)例形式分析了Java字符串及數(shù)組相關(guān)的分割、遍歷、追加等操作技巧,需要的朋友可以參考下2018-06-06
Maven dependencies與dependencyManagement的區(qū)別詳解
這篇文章主要介紹了Maven dependencies與dependencyManagement的區(qū)別詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Mybatis-Plus實(shí)現(xiàn)自定義SQL具體方法
Mybatis-Plus是Mybatis的一個增強(qiáng)工具,它可以優(yōu)化我們的開發(fā)效率,這篇文章主要介紹了Mybatis-Plus實(shí)現(xiàn)自定義SQL,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
java實(shí)現(xiàn)的導(dǎo)出Excel工具類實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的導(dǎo)出Excel工具類,結(jié)合具體實(shí)例形式分析了java導(dǎo)出Excel導(dǎo)出并生成Excel表格相關(guān)操作技巧與注意事項,需要的朋友可以參考下2017-10-10

