- Java源代码文件名:CalenderTest.java
/** * @author com.whocares@183 * @version 1.0 */ import java.util.Scanner; public class CalenderTest { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(true) { System.out.println("请输入你想要查看的2022年的月份的月历:"); int month = scan.nextInt(); String[] arr1 = new String[]{"一", "二", "三", "四", "五", "六", "七"}; for (String s : arr1) { System.out.print(s + "\t"); } int[] arr2 = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//创建一个数组来存放2022年每个月的天数 System.out.println(); int blanks = CalenderTest.blanks(month);//blanks方法返回需要留下几个空白,即打印几个星号 for (int i = 0; i < blanks; i++) {//打印前面的星号数 System.out.print("*\t"); } for (int i = 1; i <= arr2[month]; i++) {//按行打印公历日期 System.out.print(i + "\t"); blanks++; if(i == arr2[month]){ System.out.println(); } if (blanks % 7 == 0) {//满七个直接换行 System.out.println(); } } } } public static int blanks(int month){//返回值为要打印的星号数量 int days = 0, blanks = 0; if(month == 1){ return 5; } days += 31;//加上一月过的31天 if(month == 2){//到2.1过了31天。 return blanks = (days - 1) % 7 - 1; } days += 28;//加上二月过的28天 if(month == 3){ return blanks = (days - 1) % 7 - 1; } days += 31;//加上三月过的31天 if(month == 4){ return blanks = (days - 1) % 7 - 1; } days += 30;//加上四月过的30天 if(month == 5){ return blanks = (days - 1) % 7 - 1; } days += 31;//加上五月过的31天 if(month == 6){ return blanks = (days - 1) % 7 - 1; } days += 30;//加上六月过的30天 if(month == 7){ return blanks = (days - 1) % 7 - 1; } days += 31;//加上七月过的31天 if(month == 8){ return blanks = (days - 1) % 7 - 1; } days += 31;//加上八月过的31天 if(month == 9){ return blanks = (days - 1) % 7 - 1; } days += 30;//加上九月过的30天 if(month == 10){ return blanks = (days - 1) % 7 - 1; }days += 31;//加上十月过的31天 if(month == 11){ return blanks = (days - 1) % 7 - 1; }days += 30;//加上十一月过的30天 // if(month == 12){ return blanks = (days - 1) % 7 - 1; // } } }
2022年的元旦是星期六,打印输出2022年任意月份的月历(仿电脑日历的模式,仅公历)。
最新推荐文章于 2023-09-18 15:06:55 发布