java学习之路——小例子(实现输入年份,输出该年份的具体天数信息)

 
实现输入年份,输出该年份的具体天数信息。练手的代码:
 
 
package com.lcq.ThreadTest;

import java.util.Scanner;

public class Test2 {

/**
 * 功能:打印出输入年份的每一天的信息
 * @version 1.0
 * @author lcq
 */
	//countDays(int month, int year)函数用来计算这一年中每个月的天数
    int countDays(int month, int year){
    	int count = -1;
		switch(month){
		  case 1:
		  case 3:	
		  case 5:
		  case 7:
		  case 8:
		  case 10:
		  case 12:
			count = 31;
			break;
		  case 4:
		  case 6:
		  case 9:
		  case 11:
			  count = 30;
			  break;
		  case 2:
			  if(year % 4 == 0)
				  count = 29;
			  else
				  count = 28;
			  if((year % 100 ==0) & (year % 400 != 0))
					  count = 28;
		}
		return count;
	}
	public  static void main(String[] args) {
		//初始化该类
		 Test2 t = new Test2();
		 
		 System.out.println("请输入year :");
		 Scanner q=new Scanner(System.in);
		 //y 变量接受输入的year
		 int y=q.nextInt();
		 //遍历12个月份分别输出每一天
	     for (int i = 1; i <= 12; i++) {
	    	 //n变量用于标记该月的天数
	    	 int n = 0;
	    	 n = t.countDays(i,y);
	    	 for (int j = 1; j <= n; j++) {
				System.out.println(i + "月" + j +"日");
			}
		}
		 
	
	}

	

}

你可能感兴趣的:(java学习之路——小例子(实现输入年份,输出该年份的具体天数信息))