第六章第十六题(一年的天数)(Number of days in a year)

第六章第十六题(一年的天数)(Number of days in a year)

  • *6.16(一年的天数)使用下面的方法头编写一个方法,返回一年的天数:
    public static int numberOfDaysInAYear(int year)
    编写一个测试程序,显示从2000年到2020年间每年的天数
    *6.16(Number of days in a year) Write a method that returns the number of days in a year using the following header:
    public static int numberOfDaysInAYear(int year)
    Write a test program that displays the number of days in the years between 2020 and 2034.
  • 参考代码:
package chapter06;

public class Code_16 {
    public static void main(String[] args) {
        for(int i = 2000;i <= 2020;i++)
            System.out.printf("There are %d days in %d\n",numberOfDaysInAyear(i),i);
    }
    public static int numberOfDaysInAyear(int year) {
        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            return 366;
        return 365;
    }
}

  • 结果显示:
There are 366 days in 2000
There are 365 days in 2001
There are 365 days in 2002
There are 365 days in 2003
There are 366 days in 2004
There are 365 days in 2005
There are 365 days in 2006
There are 365 days in 2007
There are 366 days in 2008
There are 365 days in 2009
There are 365 days in 2010
There are 365 days in 2011
There are 366 days in 2012
There are 365 days in 2013
There are 365 days in 2014
There are 365 days in 2015
There are 366 days in 2016
There are 365 days in 2017
There are 365 days in 2018
There are 365 days in 2019
There are 366 days in 2020

Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值