zoukankan      html  css  js  c++  java
  • 闰年问题

    本周作业为写一个程序判断输入的年份是否为闰年。

    闰年有以下两种情况:

      1】输入年份%4 == 0 且 输入年份%100 != 0 为闰年;

      2】输入年份%400 == 0 为闰年。

    但是按照课上写的那段代码(见下方附)来看,使用时会有以下问题:

      1】输入的是一个字符串(如19a)

      2】输入的为负数(如-1900)

      3】输入的为空(null)的时候

      4】输入的为非常大的数超过了integer的范围

    课上写的:

    bool isLeapYear(int leap){

      return ((leap%4 == 0 && leap%100 != 0) || (leap%400 == 0))

    }

    所以为了处理这些问题,我加一个try-catch来捕捉并反馈错误。关键内容如下:

    bool isLeapYear(str s){

      ......

      try{

        int leapYear = Integer.parseInt(s);

        ..........//judge the entered number

            //include the judgement of negative number

      }catch(Exception e){

        table.setText("please enter rightly");

        ......

      }

      ......

    }

    但是有个问题。。

    如果是第四种情况,即输入一个很大的超出integer的数,就是代表很多很多很多年以后的一年是否是闰年。

    应该是可以计算这一年是否是闰年但是按照我的写法就会throw an exception。。。就不能算啦。。。

  • 相关阅读:
    [SDOI2016]排列计数
    Broken robot
    环路运输
    naptime
    Accumulation Degree
    选课
    没有上司的舞会
    金字塔
    Polygon
    石子合并
  • 原文地址:https://www.cnblogs.com/cassiecassie/p/4399181.html
Copyright © 2011-2022 走看看