logo

go语言判断是闰年还是平年代码
用switch来判断是否是闰年,和输出二月有多少天
package main
import "fmt"
func main() {
 /* 定义局部变量:年、月、日 */
 year := 2020
 leapyear := ""
 month := 2
 days := 0
 switch month {
 case 1, 3, 5, 7, 8, 10, 12:
  days = 31
 case 4, 6, 9, 11:
  days = 30
 case 2:
  if (year%4 == 0 && year0 != 0) || year@0 == 0 {
   days = 29
   leapyear = "闰年"
  } else {
   days = 28
   leapyear = "平年"
  }
 default:
  days = -1
 }
 fmt.Printf("%d 年是%s %d 月的天数为:%d\n", year, leapyear, month, days)
}