第三週實驗

判斷某一年份是否爲閏年。(如果這個年份能被4整除,但不能被100整除;或者,如果這個年份能被4整除,又能被400整除;滿足以上兩個條件中的一個的年份爲閏年)。

import java.util.Scanner;


public class LeapYear {

	int nYear;
	public LeapYear(int year)
	{
		nYear = year;
	}
	public Boolean isLeapYear()
	{
		Boolean b = false;
		if( nYear%400==0 || (nYear%4==0 && nYear%100!=0) )
		{
			b = true;
		}
		return b;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		System.out.println("please input the year:(-1 end)");
		int nYear = sc.nextInt();
		
		while(nYear>=0)
		{
			LeapYear leap = new LeapYear(nYear);
			Boolean b = leap.isLeapYear();
			if(b==true)
				System.out.println(nYear + " is Leap Year!");
			else
				System.out.println(nYear + " isn't Leap Year!");
			
			System.out.println("please input the year:(-1 end)");
			nYear = sc.nextInt();
		}
		
		
		sc.close();
	}

}


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