Go 语言程序:检查指定的年份是否为闰年


步骤

  • 获取输入的年份值。
  • 使用 if 语句检查年份是否是闰年
  • 打印最终结果。
输入要检查的年份:2016
这是闰年!
输入要检查的年份:2005
这不是闰年!

解释说明

  • 用户必须首先输入要检查的年份。
  • if 语句检查该年份是否是 4 的倍数,但不是 100 的倍数,或者是否是 400 的倍数(不是每个 4 的倍数都是闰年)。
  • 然后打印结果。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

 实时演示

package main
import "fmt"
func main() {
   var year int
   fmt.Print("Enter the year to be checked:")
   fmt.Scanf("%d", &year)
   if year%4==0 && year%100!=0 || year%400==0{
      fmt.Println("The year is a leap year!")
   }else{
      fmt.Println("The year isn't a leap year!")
   }
}

输出

Enter the year to be checked:2016
The year is a leap year!

更新日期: 31-7-2021

1K+ 浏览次数

职业生涯开始

通过完成课程获得认证

开始
广告