------------恢复内容开始------------

------------恢复内容开始------------

------------恢复内容开始------------

1.前言

知识点:Java语言的输入输出,变量的计算,浮点数的计算,if语句的使用,for循环和while循环,数组的使用,数组的计算,变量类型的转换,字符串的操作,类与类之间的调用,函数的定义,函数的调用,布尔类型的使用。

题量:前两次题目数量较多,完成的比较仓促,后面一次题目数量适中,有足够的时间完成。

难度:第一次题目挺简单,和以前写的C语言题目有点相识,只是换了种语言,大体上还是挺相识的;第二次题目最后两道题有一定的难度,需要有一定的逻辑思维才能写出来;第三次作业难度瞬间加大,每道题都要使用类与类之间的关系,对于我一个刚学Java的人来说,感觉非常难,跟摸不着头脑一样的,每道题都花费了我大量的时间,还好这次题目量少,不然很有可能完成不了这次的作业。

2.设计与分析

 

java查询某年某月工作天数_java

import  java.util.Scanner;
class Date{
   private static int year;
    private static int month;
    private static int day;
    public int getYear(){
        return this.year;
    }
    public int getMonth(){
       return this.month;
    }
    public int getDay(){
        return this.day;
    }
    public static boolean isLeapYear(int year){
        boolean i;
        if(year%400==0||(year%4==0&&year%100!=0)) i=true;
        else i=false;
        return i;    }
   public static boolean checkInputValidity(int year,int month,int day){
       boolean k=true;
       if(year>2000||year<1900||month<1||month>12||day<1||day>31) 
           { //System.out.print("Wrong Format");
             k=false;}
       if(isLeapYear(year)==true){
           if(month==2){
               if(day>29){
                //System.out.print("Wrong Format");           
                   k=false;             }}
           else if(month==4||month==6||month==9||month==11){
               if(day>30){//System.out.print("Wrong Format");
             k=false;}         }             }
       else if(isLeapYear(year)==false){
           if(month==2){
               if(day>28){
                ///System.out.print("Wrong Format");
                   k=false;    }}
         else if(month==4||month==6||month==9||month==11){
               if(day>30){//System.out.print("Wrong Format");
             k=false;}        }                }
         else  k=true;
           return k;     }
    public static void nextDate(int a,int b,int c){
        year=a;
        month=b;
        day=c;
        if(isLeapYear(a)==true){
                if(b==2) {
                    if(c+1>29) {day=c-28;
                                  month=b+1;}
                    else day=c+1;              }
               else if(b==4||b==6||b==9||b==11){
                    if(c+1>30){
                        day=c-29;
                        month=b+1;             }
                   else day=c+1;      }
               else if(b==12){
                   if(c+1>31){
                       day=1;
                       month=1;
                       year=a+1;       }
                   else day=c+1;             }
            else{
                if(c+1>31){
                    day=1;
                    month=b+1;          }
                else day=c+1;    }    
                
            }
     else {
                if(b==2) {
                    if(c+1>28) {day=c-27;
                                 month=b+1;}
                    else day=c+1;
                }
               else if(b==4||b==6||b==9||b==11){
                    if(c+1>30){
                        day=c-29;
                        month=b+1;
                    }
                   else day=c+1;
                }
               else if(b==12){
                   if(c+1>31){
                       day=1;
                       month=1;
                       year=a+1;
                   }
                   else day=c+1;
               }
            else{
                if(day+1>31){
                    day=1;
                    month=b+1;}
                else day=c+1;}         
                }   }}
public class Main{
      public static void main(String[] args){
        Scanner input=new Scanner(System.in);
          int a=input.nextInt();
          int b=input.nextInt();
          int c=input.nextInt();
          Date date=new Date();
          if(date.checkInputValidity(a,b,c)==false) System.out.print("Date Format is Wrong");
          else {
              date.nextDate(a,b,c);
              int d=date.getYear();
              int e=date.getMonth();
              int f=date.getDay();
              System.out.printf("Next day is:%d-%d-%d",d,e,f);
          }
      }
}

 

该题是判断某天的后一天的年月日,首先先输入年月日,再判断输入是否合法,如果合法,则计算输入那天后一天的年月日,

解释:该题目的难度在于首先判断它是不是闰年,其次判断判断月份,因为闰年和平年会影响每个月的天数,不同的月份有不同的天数,然后判断输入的天是不是那个月的最后一天,如果是最后一天还要判断那个月是不是那年的最后一个月份,所以需要用if一直嵌套,总体上还是挺简单的,只要思路清晰,花点时间一般都能够完成。

心得:用if语句嵌套时,需要逻辑清晰,并且每次都应该考虑else的情况。

java查询某年某月工作天数_System_02

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() + 
                        " and " + toDate.showDate() + " are:"+ fromDate.getDaysofDates(toDate)
                       );
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}

class DateUtil{
    private  int year;
    private  int month;
    private  int day;
    //DateUtil al=new DateUtil();
    public DateUtil()
    {
        
    }
    public int getYear()
    {
        return this.year;
    }
     public int getMonth()
    {
        return this.month;
    }
     public int getDay()
    {
        return this.day;
    }
    public DateUtil(int a,int b,int c)
    {
        this.year=a;
        this.month=b;
        this.day=c;
    }
    public boolean checkInputValidity(){
        int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(this.year)==true) a[2]=29;
        boolean m=true;
        if(this.year<1820||this.year>2020) m=false;
        if(this.month<1||this.month>12) m=false;
        if(this.day>a[this.month]||this.day<1) m=false;
        return m;
    }
    public boolean isLeapYear(int year)
    {
        boolean h;
        h=((year%4==0&&year%100!=0)||year%400==0);
        return h;
    }
    public DateUtil getNextNDays(int n){
        int i;
        int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(;n>0;n--)
       { if(isLeapYear(this.year)==true) a[2]=29;
        else a[2]=28;
        this.day=this.day+1;
        if(this.day>a[this.month])
        {this.day=1;
            this.month=this.month+1;
         if(this.month>12){
           this.year++;
             this.month=1;
         }
        }
        
    }
   
        return this;
    }
    public DateUtil getPreviousNDays(int n){
        int i;
       int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        for(;n>0;n--)
        { 
        if(isLeapYear(this.year)==true) a[2]=29;
            else a[2]=28;
         this.day=this.day-1;
            if(this.day==0){
                this.month=this.month-1;
               if(this.month==0){
                   this.year=this.year-1;
                   this.month=12;
                   this.day=31;
               }
              else{
                  this.day=a[this.month];
              }  
            }
      
        }return this;
    }
    //public boolean equalTwoDates(DateUtil date)
    //{    boolean h=false;
      //  if((this.year=date.getYear())&&(this.month==date.getMonth())&&(this.day==date.getDay()))
    ///        h=true;
     //return h;
   // }
    public int getDaysofDates(DateUtil date){
        int i=1,s=0,k=0;
        int y=date.getYear();
        int m=date.getMonth();
        int d=date.getDay();
        while(i==1){
            int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(this.year)==true) a[2]=29;
             if(this.year==2020&&this.month==12&&this.day==31)break;
         this.day=this.day+1;
            if(this.day>a[this.month]){
                this.day=1;
                this.month=this.month+1;
                if(this.month>12) {this.month=1;
                                   this.year=this.year+1;}
            }
            s=s+1;
           
        }
        
       while(i==1) {
            int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(y)==true) a[2]=29;
            if(y==2020&&m==12&&d==31)break;
         d=d+1;
            if(d>a[m]){
                d=1;
               m=m+1;
                if(m>12) {m=1;
                                  y=y+1;}
            }
            
            k=k+1;
        } 
     
     return s-k;
    }
   public String showDate(){
       return this.year+"-"+this.month+"-"+this.day;
   } 
    
}
该题是一个日期类设计有三个功能,分别是求前n天,后n天和两个日期相差的天数
分析:该题前两个功能和上面那题的需求有点像,上面那题求的的后一天,这题求的是后n天和前n天,我认为只要用个for循环一天一天加或一天一天减,其他和上面那道题目相似,思路都差不多,都是先判断闰年,然后再判断是第几个月,再判断是不是
那个月的最后一天或者是第一天,如果是的话,再判断该月是不是当年的最后一个月或者是第一个月,而第三个功能就不太一样,本来想用较小的日期加到更大的日期需要多少天,但是发现这样做会超时,后面发现日期是有范围的,所以让这两个日期加到
最大的那个日期看看分别需要多少天或者让这两个日期减到最小的那个日期分别需要多少天,然后计算两者相减的绝对值。
心得:如果使用for语句循环需要考虑内存超限和运行超时问题,一旦重复的次数多了就很有可能报出这两个问题。
3.踩坑心得
上面第一道题第一次提交的时候一直格式错误,后来发现一直是少了个空格,所以写pta的时候要严格按照题目的需求。
第二道题目提交了很多次才过,第一次是因为把数组放到了for循环中导致内存超限,除此之外还有两个测试点没过。

 

第二次改了之后,两个测试点过了,结果两个最大值运行超时了,现在没什么想法。后面再咨询同学之后发现它for循环里面用的是n--,而我用的是i++,结果他过了,而我没过,在把i++改成n--之后,我莫名其妙的过了,我也不知道什么原理。

java查询某年某月工作天数_java查询某年某月工作天数_03

java查询某年某月工作天数_Java_04

 

 

4.改进建议

上面第一题在提交之后,我发现如果使用if语句判断闰年和平年每个月多少天太麻烦了,我可以直接创一个数组只要判断它是不是闰年,如果是闰年,二月份就加一天,使用数组来存每个月多少天,大大简化了我的代码,使我的代码更加清晰,也更容易被其他人理解;也不需要使用那么多if语句,减少重复的判断。

上面第二题提交后,在和同学讨论方法的时候,我发现我的方法有个问题,就是我把两个日期加到范围内日期最大地那一天,各自所需要的天数我没有判断哪个数大,哪个数小,这样相减很有可能会得出一个负数,所以我用前面的数减后面的数只是碰巧通过了测试点,并不能说明我的代码是完全正确的,如果我在最后的结果前面加一个绝对值,这样结果就不会出错,这是我敲代码时没有考虑到的问题,在后面和同学分享方法的时候发现的,确实这个问题是我代码的缺陷。

5.总结

通过这三周题目集,我把语言从C语言向Java转变过来了,第一周的时候,我认为C语言和Java只是表达形式不一样而已,但通过后面的学习,通过对Java的进一步了解,我逐渐了解了C语言和Java之间的差异,而且我认为Java比C语言好用很多,在平常写题目的时候,我发现Java有许多函数是可以直接调用的,而C语言只能一个函数一个函数敲,虽然有些函数能够直接调用,但是能直接调用的函数很少,两者更大的差异是C语言是面向过程的,而Java是面向对象的,这应该是两者的最大区别,通过这三次作业,我学会使用Java语言的输入输出,变量的计算,浮点数的计算,if语句的使用,for循环和while循环,数组的使用,数组的计算,变量类型的转换,字符串的操作,类与类之间的调用,函数的定义,函数的调用,布尔类型的使用等。但是Java中还有许多好用的函数没有掌握,自己私下还应该多学习更多,这样既有益于自己平时敲代码更加简便,还能有益于自己在遇到问题不知道怎么办的时候多一点思路,除此之外还应该对Java的内在东西多一点了解,不能仅仅局限于只会敲代码,外行人也会敲代码,我们是这个专业,肯定要学与这个专业深层次的东西,这是我后面应该深入研究的地方。而对于老师的意见,我希望老师讲课的速度能够放慢一点,因为上课上的东西很多都是第一次听说的,要慢慢接受,一时间内很难反应过来,这样导致上个知识点还没有了解就直接到了下一个知识点。而对于课程来说我认为老师上完课之后应该把课件的ppt发到班级群中,这样有利于课后学习的复习。对于作业,我希望老师能够早点发作业,不要拖到一周的最后几天发,这样我们会有更多的时间安排怎样完成我们的作业。对于实验没什么意见,而对于课上和课下组织方式,我认为老师应该不仅仅是让我们听,听的时候还应该提高我们的动手能力,如上课的时候直接把电脑带去教室,讲完某个知识点的时候可以直接让我们动手实践一下,只有自己能够独立的写出来,这样才是真正的掌握,我感觉仅仅在课堂上听懂是不够的,实践才能出真知,而且,我认为课堂上能够多增加点讨论,这样能调动同学们的积极性,而且通过和同学的讨论,能够把自己不太懂的地方给搞清楚。

展望:这三周有痛苦,有快乐,有自己满意的地方,也有自己不满意的地方,总体上还行,但还有许多地方需要继续改进,还需继续加油。

 

 

该题

 

------------恢复内容结束------------

------------恢复内容结束------------