import java.util.*; import java.io.*; public class LeapYear {     public static void main(String[] args)      {                  detectLeapYear();     }     public static void detectLeapYear()     {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         String str = null;         int num = 0;         try         {             System.out.print("请输入一个年份(例如:2018),范围在【0 - " + Integer.MAX_VALUE+ "】.输入-1结束:");             while((str=br.readLine())!=null)             {                 try                 {                     num = Integer.parseInt(str.trim());                     if(num == -1)//-1作为结束标识                     {                         //退出程序                         break;                     }                     if(num>=0 && num <= Integer.MAX_VALUE)//整数范围内检测                     {                         if(isLeapYear(num))                         {                             System.out.println("公元" + num  + "是闰年");                         }                         else                         {                             System.out.println("公元" + num  + "是平年");                         }                     }                     else                     {                         System.out.println("输入的整数超出范围,重新输入");                                              }                     System.out.print("请输入一个年份(例如:2018),范围在【0 - " + Integer.MAX_VALUE+ "】.输入-1结束:");                 }catch(NumberFormatException e1)                 {                     System.out.println("输入非数字字符,请重新输入");                     System.out.print("请输入一个年份(例如:2018),范围在【0 - " + Integer.MAX_VALUE+ "】.输入-1结束:");                 }             }         }catch(IOException e)         {         }finally         {             if(br != null)         {             try             {                 br.close();             }catch(IOException e)             {}         }         }              }     //检测是否是闰年     public static boolean isLeapYear(int year)     {         return (((year%4==0)&&(year%100!=0)) || year%400==0) ? true :false;     } }
点赞 评论

相关推荐

明天不下雨了:这个项目 这个简历 这个模板 莫不是一个开源的
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务