赞
踩



public class JudgeDate { public static boolean judge(int y,int m,int d) { boolean p=false; if(m<1||m>12) { System.out.print("月份不合法"); p=false;} else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) { if(d<=31) { p=true;} else { p=false; System.out.print("日期不合法"); } } else if(m==2) { if(y%400==0||(y%4==0&&y%100!=0)) { if(d<=29) { p=true; } else { p=false; System.out.print("日期不合法"); } } else { if(d<=28){ p=true; } else { p=false; System.out.print("日期不合法"); } } } else { if(d<=30){ p=true; } else { p=false; System.out.print("日期不合法"); } } return p; } public static void main(String[] args) { // TODO 自动生成的方法存根 @SuppressWarnings("resource") Scanner s=new Scanner(System.in); int y=s.nextInt(); int m=s.nextInt(); int d=s.nextInt(); if(judge(y,m,d)) { System.out.print(y+"/"+m+"/"+d); } } }

public class JudgeDate { static int year=2000; static int month; int day; public JudgeDate(int y,int m,int d) { year=y; if(m<1||m>12) { month=1; } else { month=m; } day=checkDay(d); } public String toString() { return year+"/"+month+"/"+day; } public static int checkDay(int d) { int mouthDay[]= {31,28,31,30,31,30,31,31,30,31,30,31}; if(month==2&&d<=29&&(year%400==0||(year%4==0&&year%100!=0))){ return d; } else { if(d>mouthDay[month-1]) { d=mouthDay[month-1]; } } return d; } public static void main(String[] args) { // TODO 自动生成的方法存根 JudgeDate d1=new JudgeDate(2020,01,32); System.out.println(d1); JudgeDate d2=new JudgeDate(2020,12,24); System.out.println(d2); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。