从键盘输入一个年份(如2000年)和一个月份,输出该月的天数

从键盘输入一个年份(如2000年)和一个月份,输出该月的天数

参考chatgpt以及个人思路:

  • 首先通过Scanner类获取控制台输入的年份和月份。
  • 然后调用getDaysOfMonth()方法计算该月的天数。
  • 在getDaysOfMonth()方法中,根据月份的不同,使用switch语句计算该月的天数。
  • 对于2月份特别处理,根据闰年和非闰年的情况计算天数。
  • 最后输出结果。
    import java.util.Scanner;
    public class Main {
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          System.out.print("请输入年份:");
          int year = sc.nextInt();   
          System.out.print("请输入月份:");
          int month = sc.nextInt();
          int days = getDaysOfMonth(year, month);      
          System.out.println(year + "年" + month + "月有" + days + "天");
      }
      public static int getDaysOfMonth(int year, int month) {
          int days = 0;
          switch (month) {
              case 1:
              case 3:
              case 5:
              case 7:
              case 8:
              case 10:
              case 12:
                  days = 31;
                  break;
              case 4:
              case 6:
              case 9:
              case 11:
                  days = 30;
                  break;
              case 2:
                  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                      days = 29;
                  } else {
                      days = 28;
                  }
                  break;
              default:
                  System.out.println("请输入正确的月份!");
                  break;
          }
          return days;
      }
    }
    

参考下

 public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("请输入年");
            int year = scanner.nextInt();
            System.out.println("请输入月");
            int month = scanner.nextInt();
            int length = Month.of(month).length(Year.isLeap(year));
            System.out.println(String.format("%s年的%s月有%s天" ,year ,month ,length));
        }
    }

具体代码如下,如有疑问及时反馈,自己在本地写完跑了是没问题的哈,你可以参考下:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入年份:");
        int year = scanner.nextInt();

        System.out.print("请输入月份:");
        int month = scanner.nextInt();

        int days = 0;

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            default:
                System.out.println("输入的月份有误!");
                System.exit(0);
        }

        System.out.println(year + "年" + month + "月有" + days + "天。");
    }
}

如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^