BUPT - Java日历题目

  1. 从按制台接收用户的输入的出生日期,输出从用户出生的那一年到今年为止,每年的生日当月的日历,生日当天标*号,并统计这么多年用户生日当天为一周中每一天的百分比(即生日为周一的在这么多年中占的百分比,生日为周二的在这么多年中占的百分比。。。。。)

 (照片统计周几天数的错位了,应该向前移动一位)

心得:日历那个表格:(,想了半天,还有最后日期的统计,要注意几号和周几的对应,建议自己写写循环看看。

import java.util.Calendar;
import java.util.Scanner;


public class year {
        public static void printCalender() {
            Scanner in=new Scanner(System.in);
            int y = in.nextInt();//当前年份
            int m =in.nextInt();。。当前月份
            int d=in.nextInt();//确定的日期,具体的日子
            int  cnt=0;
            double[]p={0,1,2,3,4,5,6,};//判断
            double[]np=new double[7];
            //input
            for(int k=y;k<=2022;k++){//这里写的是当前的2022,可以自定义输入
                System.out.println();
                System.out.println(k);
            Calendar calendar = Calendar.getInstance();
            calendar.set(k, m, d);
            int startDay = calendar.get(Calendar.DAY_OF_WEEK); //求本周第一天是星期几
                //System.out.println(startDay);//这里可以自己看看当前startDay到哪里了
            int count = startDay - 1; //第一周的初始计数
            int maxDay = maxDayInMonth(k, m);

                int hhh=count;//计算几号和周几的对应关系
                for(int j=1;j<d;j++){
                    hhh++;
                    if(hhh>6) hhh=0;//归零,周六==6,周天==0
                }
                for(int i=0;i<7;i++){
                    if(p[i]==hhh){
                        np[(int) p[i]]++;
                    }//对应周几,并累积起来

                }//循环检索周几...++;

            System.out.println("周日 周一 周二 周三 周四 周五 周六");
            fillSpace(startDay);//填上空白

            for (int i=1; i <=maxDay; i++) {

                if(i==d)
                    System.out.printf("%3d%s",i,"*","");

                else System.out.printf("%3d%s", i," ");//使用printf函数进行格式对齐
                count++;
                if (count>=7) { //每输出7天换一次行
                    count=0;
                    System.out.print('\n');
                }
            }
             cnt++;//年数
        }
            System.out.println();
            System.out.println("星期天:"+String.format("%.3f",np[0]/cnt));//浮点输出,保留三位小数!
            System.out.println("星期一:"+String.format("%.3f",np[1]/cnt));
            System.out.println("星期二:"+String.format("%.3f",np[2]/cnt));
            System.out.println("星期三:"+String.format("%.3f",np[3]/cnt));
            System.out.println("星期四:"+String.format("%.3f",np[4]/cnt));
            System.out.println("星期五:"+String.format("%.3f",np[5]/cnt));
            System.out.println("星期六:"+String.format("%.3f",np[6]/cnt));
        }
        public static void fillSpace(int startDay) {
            for (int i = 1; i< startDay; i++) {
                System.out.printf("%4s", " ");//置位符
            }
        }
        public static int maxDayInMonth(int year, int month) {
            int mm = 30;
            if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12)
                mm = 31;
            else if (month == 2)
                mm = 28;
            else if ((month == 2) & (year % 4 == 0) && (year % 100!=0 )|| (year % 400 == 0))
                mm = 29;
            return mm;
        }//计算出月份的大小

    public static void main(String[] args){
        System.out.println("请输入年月日: (格式:2007 12 3)");
        printCalender();//输出
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值