zoukankan      html  css  js  c++  java
  • 判断闰年

    1.目标:根据输入的年月日判断是否为闰年

    2.代码:

     1 import java.util.Scanner;
     2 public class Date {
     3     private int year; //
     4     private int month; //
     5     private int day; // 6     //有默认值的构造函数
     7     public Date() {
     8         year = 1000;
     9         month = 1;
    10         day = 1;
    11     }
    12     // 判断是否为闰年
    13     public int isleap() {
    14         if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
    15             return 1;
    16         } else {
    17             return 0;
    18         }
    19     }
    20     // 判断日期是否合法
    21     public int check() {
    22         int m, n = 2;
    23         m = isleap();
    24         if (month > 12) {
    25             n = 0;
    26         }
    27         if (month < 0 || year < 0 || day < 0) {
    28             n = 0;
    29         }
    30         if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
    31             if (day > 31)
    32                 n = 0;
    33             else
    34                 n = 1;
    35         }
    36         if (month == 4 || month == 6 || month == 9 || month == 11) {
    37             if (day > 30)
    38                 n = 0;
    39             else
    40                 n = 1;
    41         }
    42         if (m == 1) {
    43             if (month == 2 && day >= 30)
    44                 n = 0;
    45             if (month == 2 && day < 30)
    46                 n = 1;
    47         }
    48         if (m == 0) {
    49             if (month == 2 && day >= 29)
    50                 n = 0;
    51             if (month == 2 && day < 29)
    52                 n = 1;
    53         }
    54         return n;
    55     }
    56     public void setDate() {
    57         Scanner in = new Scanner(System.in);
    58         System.out.print("请输入年月日:");
    59         year = in.nextInt();
    60         month = in.nextInt();
    61         day = in.nextInt();
    62     }
    63     public void display() {
    64         System.out.println(year + "-" + month + "-" + day);
    65         int a, b;
    66         a = isleap();
    67         b = check();
    68         if (b == 0) {
    69             System.out.println("Error Date!");
    70         } else if (b == 1) {
    71             if (a == 1)
    72                 System.out.println("Leap year");
    73             if (a == 0)
    74                 System.out.println("Common year");
    75         }
    76     }
    77     public static void main(String[] args) {
    78         Date d1 = new Date();
    79         Date d2 = new Date();
    80         d1.display();
    81         d2.setDate();
    82         d2.display();
    83     }
    84 }

    3.测试截图

  • 相关阅读:
    static 关键字
    gitlab 配置到jenkins
    Eclipse下代码字体背景变红/变绿/变黄原因
    构造方法(和python初始化变量类似)
    面向对象(实际就像python跳用自己写的库那样)
    Python 的AES加密与解密
    break 和 continue 的用法
    for循环
    Eclipse快捷键
    java三元运算符
  • 原文地址:https://www.cnblogs.com/znjy/p/13991970.html
Copyright © 2011-2022 走看看