Java——某年某月的天数

青旅半醒 2022-12-12 13:41 232阅读 0赞

Java——某年某月的天数

Description
输入年和月,判断该月有几天?
Input
输入年和月,格式为年\月。
Output
输出该月的天数。
Sample
Input
2009\1
Output
31
Hint
注意判断闰年啊

  1. import java.util.*;
  2. public class Main {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. Scanner reader=new Scanner (System.in);
  6. String str;
  7. str=reader.nextLine();
  8. String s[]=str.split("\\\\");
  9. int year=Integer.valueOf(s[0]);
  10. int month=Integer.valueOf(s[1]);
  11. int days=0;
  12. int flag=0;
  13. if((year%4==0&&year%100!=0)||year%400==0)
  14. flag=1;
  15. if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  16. days=31;
  17. else if(month==4||month==6||month==9||month==11)
  18. days=30;
  19. else
  20. {
  21. if(flag==1)
  22. days=29;
  23. else
  24. days=28;
  25. }
  26. System.out.print(days);
  27. }
  28. }

Integer valueOf(int i)返回一个表示指定的 int 值的 Integer 实例。如果不需要新的 Integer 实例,则通常应优先使用该方法,而不是构造方法 Integer(int),因为该方法有可能通过缓存经常请求的值而显著提高空间和时间性能。
在输入数据的时候要用Java的split函数做字符串
的分割,但是输入的数据中存在‘\’,这就需要转义来辨别,在Java中用\\来代替\

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读