public static int numberOfDaysInAYear(int year)
Write a test program that displays the number of days in year from 2000 to 2020.
public class C1
{
public static void main(String[] args)
{
int year = 2000;
for(int i = 1; i <= 21 ; i++ )//印出2000-2020共跑21次
{
System.out.printf("%-4d",year);//印出年分
System.out.print("年 ");
System.out.println(numberOfDaysInAYear(year) + "天");
//呼叫 numberOfDaysInAYear 並回傳天數
year = year + 1;
}
}
public static int numberOfDaysInAYear(int year)
{
//判斷天數
int a = year % 4;
int b = year % 100;
int c = year % 400;
int day = 365 ;
if(a == 0 || c == 0 && b != 0 )//為閏年
{
day = 366 ;
}
else//不為閏年
{
day = 365 ;
}
return day;//回傳結果
}
}
沒有留言:
張貼留言