赞
踩
题目:输出万年历
提示:1900年1月1日是星期一
import java.util.Scanner; public class Calendar { public static void main(String[] args){ Help util =new Help(); Scanner sc= new Scanner(System.in); System.out.println("请输入年"); int year = sc.nextInt(); boolean ynleapyear = Help.ynleapyear(year); // System.out.println("是否是闰年"+ynleapyear); Scanner sc1= new Scanner(System.in); System.out.println("请输入月"); int month = sc1.nextInt(); int getMonthdays = Help.getMonthdays(year, month); // System.out.println("当前月有几天:"+getMonthdays); int yeartotaldays =Help.yeartotaldays(year);//每一年总天数 int totalmonthdays=Help.totalmonthdays(year, month);//当前年输入月前面月份的总天数 int sumdays = yeartotaldays+totalmonthdays;//总天数 //System.out.println("总天数:"+sumdays); int firstday =Help.firstday(sumdays);//当前月第一天是周几 //System.out.println("第一天是:"+firstday); System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六"); int i =1; if(i!=7){ for (i=0;i<firstday;i++){ System.out.print("\t"); } } for (i=1;i<=getMonthdays;i++){ System.out.print(i+"\t"); if((sumdays+i+1)%7==0&&i!=1){//换行i=1时会直接换行 System.out.println(""); } } } } public class Help { /** * 判断是否是闰年 */ public static boolean ynleapyear(int year) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { return true; } return false; } /** * 获取当前月有几天 */ public static int getMonthdays(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; default: if (ynleapyear(year)) { return 29; } else { return 28; } } } /** * 获取1900年到输入年前一年的总天数 */ public static int yeartotaldays(int year) { int days = 0; for (int years = 1900; years < year; years++) if (ynleapyear(years)) { days += 366; } else { days += 365; } return days; } /** * 获取输入年一月到输入月前一月总天数,可以提取方法getMonthdays */ public static int totalmonthdays(int year, int month) { int days = 0; int i = 0; for (i = 1; i < month; i++) { days += getMonthdays(year, i); } return days; } /** * 判断第一天是周几 */ public static int firstday(int sumdays) { int firstday = (sumdays % 7) + 1; return firstday; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。