久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Java實現簡單的萬年歷

瀏覽:133日期:2022-08-14 10:04:08

本文實例為大家分享了Java實現簡單萬年歷的具體代碼,供大家參考,具體內容如下

1 要求

1、輸入年份;2、輸入月份;3、輸出某年某月的日歷。

2 思路

1、實現從控制臺接收年和月,判斷是否是閏年(判斷是否是閏年:能被4整除但不能被100整除;或者能被400整除);

2、計算輸入月份的天數;

3、計算該月第一天是星期幾;

3.1 計算輸入年份距離1900年1月1日的天數;3.2 計算輸入月份之前的天數(從當年年初開始);3.3 將以上兩組數據進行求和;3.4 已知該月之前的天數,計算輸入月份的第一天是星期幾(從1900年1月1日(星期一)開始推算: 星期幾 = 1 + 天數差 % 7 )。

4、按格式輸出該月日歷 。

3 源代碼

import java.util.Scanner;public class index { //每個月的天數 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];} } //月份總天數 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年的年份總天數 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年的總天數'+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日總天數totaldays = monthdays(month,year)+yeardays(year);System.out.println(year+'年'+month+'月1日距離1900年的總天數:'+totaldays);System.out.println('n**********'+year+'年'+month+'月的日歷為**********');System.out.println('一t二t三t四t五t六t日t');int week = 1+totaldays%7;//根據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('輸入的年份不合法!');} } //主函數 public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println('***********************歡迎使用萬年歷**************************');System.out.println('*********請選擇你需要進行的操作(輸入進行操作之前的數字)**********');System.out.println('********************1.查詢某年某月的日歷************************');System.out.println('********************2.結束操作*********************************');System.out.print('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請選擇你需要進行的操作:'); a = scanner.nextInt(); break;case 2: System.out.println('退出程序成功!'); return; }} }}4 結果截圖

Java實現簡單的萬年歷

注意:我的周日是在最后一欄

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 稻城县| 惠来县| 三都| 芒康县| 徐闻县| 自治县| 高邑县| 彭阳县| 乌恰县| 永顺县| 逊克县| 天峻县| 绿春县| 吉安市| 六枝特区| 鹤岗市| 九龙坡区| 瓦房店市| 南雄市| 察哈| 绥化市| 兖州市| 丽江市| 阳曲县| 蒙城县| 海口市| 贡觉县| 赤水市| 安岳县| 麦盖提县| 密云县| 肇东市| 延寿县| 阿尔山市| 乌海市| 五寨县| 祥云县| 云南省| 肇源县| 旬阳县| 宣城市|