• 第一个小型题目:日历


    import java.util.Scanner;

    public class canlendar {

     /**
      * 判断是否为闰年
      *
      * @param year
      * @return
      */
     public static boolean isLeapYear(int year) {
      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
       return true;
      } else
       return false;

     }

     /**
      * 根据传入的年月,计算该月有多少天
      *
      * @param year
      * @param month
      * @return
      */
     public static int monthOfDays(int year, int month) {
      if (month == 2) {
       if (isLeapYear(year)) {
        return 29;
       } else
        return 28;
      } else if (month < 7 && month % 2 != 0 || month > 7 && month % 2 == 0) {
       return 31;
      } else
       return 30;

     }

     /**
      * 根据提供的年月,计算是该月1号是该年中的第几天
      *
      * @param year
      * @return
      */
     public static int crossDays(int year,int month) {
      int sumCrossDays = 0;
      for (int i = 1; i < month; i++) {
       sumCrossDays += monthOfDays(year, i);
      }
      return sumCrossDays;

     }

     /**
      * 求1900年到輸入的年份一共多少天
      * @param year
      * @return
      */
     public static int newCrossDays(int year) {
      int sumdays = 0;
      for (int i = 1900; i < year; i++) {
       if (isLeapYear(i)) {
        sumdays += 366;
       } else
        sumdays += 365;
      }
      return sumdays;
     }

     
     
     public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      
      System.out.println("请输入一个年份:");
      int years = scan.nextInt();
      System.out.println("请输入一个月份:");
      int month = scan.nextInt();
      int sumDays = newCrossDays(years);
      System.out.println("請輸入一個日期:");
      int day = scan.nextInt();
      System.out.println("一 二 三 四 五 六 七"); 
      int week=1;;
      week=(newCrossDays(years)+crossDays(years,month)+day+1)%8+1;
       //System.out.println("你輸入的是星期"+week);
      int sum=week+monthOfDays(years,month);
      int[] dates=new int[sum];
      for(int k=0;k<=week;k++){
       dates[k]=-1;
      }
      int index=1;
      for(int number=week;number<sum;number++){
       dates[number]=index;
       index++;
      }
      for(int k=0;k<dates.length;k++){
       if(dates[k]==-1){
        System.out.print(" ");
       }else
        System.out.print(dates[k]+" ");
       if((k+1)%7==0){
        System.out.println();
      }
      }
      
       

     }
    }

  • 相关阅读:
    跟着思兼学习Klipper(16) Klipper 网页使用摄像头比较全指南
    Ubuntu系统Apache2安装后无法启动,报错apache2.service: Control process exited, code=exited status=1
    正则表达式内容和涵义
    浅谈vue中render函数
    IE浏览器报错TypeError: 对象不支持“includes”属性或方法,其他浏览器正常显示
    vue项目使用svg的步骤和bug过程
    postgresql 数据库备份与恢复
    魔法上网🚁
    [论文] BBR:基于拥塞(而非丢包)的拥塞控制(ACM, 2017)
    NAT 穿透是如何工作的:技术原理及企业级实践(Tailscale, 2020)
  • 原文地址:https://www.cnblogs.com/smile-zcc/p/5559716.html
Copyright © 2020-2023  润新知