计算机技术课实验报告(6)

实验目的:

从键盘上输入一个年份,并输入一个月份(数字),输出该月份有多少天。

实验代码:

public class daiMa6 {
	public static void main(String[] args) {
        System.out.println("请输一个年份:");
        int year = Console.readInt();
        
        int yearCharge;
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
        	yearCharge = 1;
        }
        else
        	yearCharge = 0;

        System.out.println("请输一个月份:");
        int month = Console.readInt();
        
        if(month > 12 || month < 0)
        {
        	 System.out.println("不存在该月!");
        	 
        }
        else if(month == 1 || month == 3 || month  == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
        	System.out.println("该月份有31天");
        }
        else if(month == 2)
        {
        	if(yearCharge == 1)
        	{
        		System.out.println("该月份有29天");
        	}
        	else
        		System.out.println("该月份有28天");
        	
        }
        else
        	System.out.println("该月份有30天");
	}
}


 

实验结果:

请输一个年份:
2000
请输一个月份:
2
该月份有29天


实验心得:
总体来说需要注意两个方面吧,其一:自然是判断当前年份是平年还是闰年的问题,当然,前面有做过,所以不会太成问题;其二:就是输出该月有多少天,当然不会是单纯的是说输出天数,是要注意2月份到底有多少天,这自然要根据输入的年份来决定,所以啊,这就将判断闰年平年联系到了一起,所以要注意喽。其他的,没有什么,就是注意不要因为粗心不小心写错代码就好了。

 

你可能感兴趣的:(计算机技术课实验报告(6))