飞道的博客

Java输入年度和月份判断有多少天

301人阅读  评论(0)

  
  1. package com.ambow.www.ch03;
  2. import java.util.Scanner;
  3. public class Day {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print( "请输入年份");
  7. int year = sc.nextInt();
  8. System.out.print( "请输入月份");
  9. int month = sc.nextInt();
  10. if(month< 0 || month> 12 || year< 0) {
  11. System.out.println( "请输入合法的年月!");
  12. } else {
  13. switch(month) {
  14. case 1:
  15. case 3:
  16. case 5:
  17. case 7:
  18. case 8:
  19. case 10:
  20. case 12:System.out.println( "31天"); break;
  21. case 4:
  22. case 6:
  23. case 9:
  24. case 11:System.out.println( "30天"); break;
  25. }
  26. if((year% 100!= 0&&year% 4== 0)||year% 400== 0) {
  27. if(month== 2) {
  28. System.out.println( "29天");
  29. }
  30. } else {
  31. if(month== 2) {
  32. System.out.println( "28天");
  33. }
  34. }
  35. }
  36. }
  37. }

 


转载:https://blog.csdn.net/kuailexiaohan/article/details/110497606
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场