打印全年日历 java实现

问题描述

编写程序,提示用户输人年份和代表该年第一天是星期几的数字,然后在控制台上 显示该年的日历表

问题分析

首先我们要知道如何判断闰年
如何判断闰年的函数java实现
其次 我们要知道如何格式化输出 后面再提到
其他的就是一些基础的循环知识

代码

private static void nPrint_funtion(int year, boolean is, int number) {
		// TODO Auto-generated method stub
		int month;
		for (int i = 1; i <= 12; i++) {
			System.out.println("                " + year + "年" + i + "月");
			if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
				month = 31;
			else if (i == 2 && is)
				month = 28;
			else if (i == 2 && is == false)
				month = 27;
			else
				month = 30;
			System.out.println("------------------------------------------------------");
			System.out.println("Sun     Mon     Tut     Wod     Thu     Fri     Sat ");
			for (int j = 1; j <= month; j++) {
				if (j == 1) {
/*					for (int j1 = 0; j1 < number * 8; j1++) {
						System.out.print(" ");

					}
					System.out.print(j);*/
					//第二种
					System.out.printf("%"+(number*8+1)+"d",j);
					if ((number + j) % 7 == 0) {
						System.out.println();
					} else
						System.out.print("       ");
				} else {
					if ((number + j) % 7 == 0) {
						System.out.println(j);
					} else {
						if (j > 9)
							System.out.print(j + "      ");
						else
							System.out.print(j + "       ");
					}
				}

			}
			number = (number + month) % 7;

			System.out.println();
			System.out.println("=======================================================");
			System.out.println();
			System.out.println();

		}
	}

重点代码

因为一开始对printf不熟悉,就使用了循环打印的方式,其实使用printf更加方便,printf的用法
如%5d 是输出占位为5的int数字右对齐
而%-5d是输出占位为5的int数字左对齐
注意的是数字是包括在占位里的。

猜你喜欢

转载自blog.csdn.net/qq_40435621/article/details/83511709