java基礎_從控制檯輸入年份月份,打印該月有多少天

/*
從控制檯輸入年份月份,打印該月有多少天?(區分平年與閏年的2月)
*/
import java.util.Scanner;

public class TianShu {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int year;
		int month;
		Scanner sc=new Scanner(System.in);
		System.out.println("請輸入年份:");
		year=sc.nextInt();
		System.out.println("請輸入月份:");
		month=sc.nextInt();
		
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
		{
			System.out.println(year+"年"+month+"月有31天");
		}
		if(month==4||month==6||month==9||month==11)
		{
			System.out.println(year+"年"+month+"月有30天");
		}	
		if(month==2)
		{
			if((year%400==0)||(year%4==0&&year%100!=0))
			{
				System.out.println(year+"年"+month+"月有29天");
			}
			else
			System.out.println(year+"年"+month+"月有28天");
		
		}			
		
		
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章