题目集1~3的总结性Blog

发布时间 2023-03-26 19:48:23作者: TTZYLL

题目集1:

1、计算年利率

2、身体质量指数测算

3、九九乘法表(双循环)

4、快递运费

5、去掉重复字符

6、统计一个子串在整串中出现的次数

7、有重复数据

8、从一个字符串中移除包含在另一个字符串中的字符

9、Prime Numbers

10、GPS数据处理

11、求定积分

12、列出最简真分数序列*

题目集2

1、长度质量计算单位换算

2、奇数求和

3、房产税计算2022

4、游戏角色选择

5、学号识别

6、巴比伦法求平方根近似值

7、二进制数值提取

8、判断三角形类型

9、求下一天

题目集3

1、创建圆类型

2、创建账户类Account

3、定义日期类

4、日期类设计

1、前言:

   第一次使用java写一些题目,虽然老师在布置题目时说题目很简单,叫我们尽早完成,但对于我们这种刚接触java这门语言的人,有的题目还是有着一定的难度,当时还问了室友很久,什么叫主方法所在的类必须命名为Main,搞出了不少笑话。

  题目集一中题目量蛮大,但是难度倒还是我们可以接受的程度,其中印象比较深刻的就是第七题:有重复数据和第十题:GPS数据处理,这两题在完成时,给我带来了困扰和疑惑。

  题目集二难度也不是很大,但是完成肯定也是要花费我们一定时间,值得一说的还得是第五题:学号识别,第八题:判断三角形类型和第九题:求下一天。

  题目集三,是我们对类的第一次运用,虽然在课和慕课上有所讲解,但我在第一次运用上还是出现了很多问题,当然老师设置题目也是有一种循序渐进的意味在,通过第一和第二题,让我们先对类有个大致的了解和大致的掌握,其中的第二题:创建账户类Account、第三题:定义日期类和日期类设计,都值得细细深入。

2、设计与分析:

 有重复数据处理

  题目集一里面开始对我有困扰的题,原本我以为是有什么特殊的方法,可以快速得出结果,当时就思考少了,去网上查了一下,便有了老师所说的用大炮打蚊子,非常的尬。后面自己又对代码进行了改进。

有重复的数据

在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。

import java.util.Scanner;
import java.util.HashSet;
import java.util.Set;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        input.nextLine();
        String s1 = input.nextLine();
        String[] s = s1.split(" ");
        Set<String>pre = new HashSet<String>();
        for(int i = 0;i<s.length;++i){
            pre.add(s[i]);
            if(pre.size()!=i+1){
                System.out.print("YES");
                return;
            }
        }
        System.out.print("NO");
    }
}

上面是我原本写的代码,后面听老师说后,自己又改了代码,如下:

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int array[]=new int[n];
        for(int i=0;i<n;i++){
            array[i]=input.nextInt();
        }
        Arrays.sort(array);
        if(n==1)System.out.print("NO");
        else{
            for(int i=0;i<n-1;i++){
                if(array[i]==array[i+1]){
                    System.out.print("YES");
                    return ;
                }
                if(i==n-2)
                    System.out.print("NO");
            }
        }
    }
}

GPS数据处理

NMEA-0183协议是为了在不同的GPS(全球定位系统)导航设备中建立统一的BTCM(海事无线电技术委员会)标准,由美国国家海洋电子协会(NMEA-The National Marine Electronics Associa-tion)制定的一套通讯协议。GPS接收机根据NMEA-0183协议的标准规范,将位置、速度等信息通过串口传送到PC机、PDA等设备。

NMEA-0183协议是GPS接收机应当遵守的标准协议,也是目前GPS接收机上使用最广泛的协议,大多数常见的GPS接收机、GPS数据处理软件、导航软件都遵守或者至少兼容这个协议。

NMEA-0183协议定义的语句非常多,但是常用的或者说兼容性最广的语句只有$GPGGA、$GPGSA、$GPGSV、$GPRMC、$GPVTG、$GPGLL等。

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String s1=null,time = null;
        while (input.hasNext()){
            s1=input.nextLine();
            if(s1.equals("END")){
                break;
            }
            int sum=0;
            String []split=s1.split(",");
            if(split[0].equals("$GPRMC")){
                int end=s1.indexOf("*");
                for(int i=1;i<end;i++){
                    sum^=(int)s1.charAt(i);
                }
            sum%=65536;
            Boolean flag=split[2].equals("A");
            Boolean S=sum==Integer.parseInt(s1.substring(end+1),16);
            if(S==true&&flag==true){
                time=split[1];
            }
        }
    }
    if(time!=null){
        int h=(Integer.parseInt(time.substring(0,2))+8)%24;
        String m=time.substring(2,4);
        String s=time.substring(4,6);
        if(h<10)
            System.out.print("0");
        System.out.print(h+":"+m+":"+s);
        }
    }
}

这道题在完成过程中还与室友有过一段讨论,北京时间要在世界时间上加上8个小时。

学号识别

学校的学号由8位数字组成,前两位是入学年份(省略了20);第3、4位是学院编号,01代表材料学院,02代表机械学院,03代表外语学院,20代表软件学院;第5、6位是学院内部班级编号,最后两位是班级内部学号。如:18011103,入学年份是2018年,材料学院,11班,03号

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        String s1=null;
        if(s.length()!=8){
            System.out.print("Wrong Format");
        }
            else{
            switch(s.substring(2,4)){
                case "01":
                    s1="材料学院";
                    System.out.println("入学年份:20"+s.substring(0,2)+"年");
                    System.out.println("学院:"+s1);
                    System.out.println("班级:"+s.substring(4,6));
                    System.out.print("学号:"+s.substring(6,8));
                    break;
                case "02":
                    s1="机械学院";
                    System.out.println("入学年份:20"+s.substring(0,2)+"年");
                    System.out.println("学院:"+s1);
                    System.out.println("班级:"+s.substring(4,6));
                    System.out.print("学号:"+s.substring(6,8));
                    break;
                case "03":
                    s1="外语学院";
                    System.out.println("入学年份:20"+s.substring(0,2)+"年");
                    System.out.println("学院:"+s1);
                    System.out.println("班级:"+s.substring(4,6));
                    System.out.print("学号:"+s.substring(6,8));
                    break;
                case "20":
                    s1="软件学院";
                    System.out.println("入学年份:20"+s.substring(0,2)+"年");
                    System.out.println("学院:"+s1);
                    System.out.println("班级:"+s.substring(4,6));
                    System.out.print("学号:"+s.substring(6,8));
                    break;
                default:
                    System.out.print("Wrong Format");
            }
        }
    }
}

这道题我在完成时,遇到了输出的问题,因为入学年份,班级,学号,都是可以根据,我们的输入直接得出,而学院却是要经过我们的判断,来输出是什么学院,而且学院所处位置是在所有输出之间,不是在开头或者最后,所以我只能写了四个输出语句,在刚开始写这道题时,还给我带来一定困扰,为什么这个输出与众不同。

判断三角形类型

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();
        if(a<1||a>200||b<1||b>200||c<1||c>200)
            System.out.print("Wrong Format");
        else if(a>=b+c||c>=b+a||b>=a+c)
            System.out.print("Not a triangle");
        else if(a==b&&b==c&&a==c)
            System.out.print("Equilateral triangle");
        else if(a==b||b==c||a==c)
        {
             if(Math.abs(a*a-(b*b+c*c))<0.1||Math.abs(b*b-(a*a+c*c))<0.1||Math.abs(c*c-(a*a+b*b))<0.1)
                System.out.print("Isosceles right-angled triangle");
            else
                System.out.print("Isosceles triangle");
        }
        else if(a*a==(b*b+c*c)||b*b==(a*a+c*c)||c*c==(a*a+b*b))
            System.out.print("Right-angled triangle");
        else
            System.out.print("General triangle");
    }
}

对于这道题目,我在写的时候,考虑的就是范围从大到小,先是超出限制范围,输入错误,再到输入数据无法构成三角形,再就是最特殊的等边三角形,然后就是等腰三角形,判断该等腰三角形是否为直角等腰三角形或者是普通的等腰三角形,再到直角三角形和普通的三角形。在完成这道题时,开始没有考虑到输入数据时,所输入数可能不够精确,如2,2,2√2是等腰直角三角形,可是我们无法输入2√2这种十分准确的数,所以在考虑是否为直角三角形时,用到if(Math.abs(a*a-(b*b+c*c))<0.1||Math.abs(b*b-(a*a+c*c))<0.1||Math.abs(c*c-(a*a+b*b))<0.1),使之精确度减小,得到正确结果。

求下一天

 

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        int month = input.nextInt();
        int day = input.nextInt();
        nextDate(year,month,day);
    }
    public static boolean isLeapYear(int year){
        boolean isLeapYear = (year%4==0&&year%100!=0)||year%400==0;
        return isLeapYear;
    }
    public static boolean checkInputValidity(int year,int month,int day){
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
        boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=s[month]&&day>0);
        return checkInputValidity;
    }
    public static void nextDate(int year,int month,int day){
        int[] s=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
        int a=0;
        int b=0;
        int c=0;
        if(checkInputValidity(year,month,day)) {
            if(month==12) {
                if(day==s[month]) {
                a = year+1;
                b = 1;
                c = 1;}
            if(day>0&&day<s[month]) {
                a = year;
                b = month;
                c =day +1;}
            }
            if(month<12) {
                if(day==s[month]) {
                    a = year;
                    b = month + 1;
                    c = 1;}
                if(day>0&&day<s[month]) {
                    a = year;
                    b = month;
                    c = day+1;}
            }
            System.out.println("Next date is:"+a+"-"+b+"-"+c);
            }
        else
            System.out.println("Wrong Format");
    }
}

 

这道题就是一个简单的运算方面的问题,考虑到是否为润年,考虑到每个月的天数可能不同,在到达每年的最后一天year+1,month=1,day=1;每个月的最后一天,month+1,day=1;就是考虑到这方面就能轻松解决这道题。

创建账户类Account

import java.util.Scanner;
import java.time.LocalDate;
public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int id=input.nextInt();
        double balance=input.nextDouble();
        double annuallnterestRate=input.nextDouble();
        double withdraw1= input.nextDouble();
        double deposit1=input.nextDouble();
        double newbalance;
        Account a=new Account(id,balance);
        if(a.withDraw(withdraw1)==true&&a.deposit(deposit1)==true)
        {
            newbalance=balance-withdraw1+deposit1;
            System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
            System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlyInterestRate(newbalance,annuallnterestRate)));
            a.getDateCreated();
        }
        if(a.withDraw(withdraw1)==false||a.deposit(deposit1)==false)
        {
            newbalance=balance;
            if(a.withDraw(withdraw1))
                newbalance=newbalance-withdraw1;
            else
                System.out.println("WithDraw Amount Wrong");
            if(a.deposit(deposit1))
                newbalance=newbalance+deposit1;
            else
                System.out.println("Deposit Amount Wrong");
            System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
            System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlyInterestRate(newbalance,annuallnterestRate)));
            a.getDateCreated();
        }
    }
}
class Account{
    private int id;
    private double balance;
    private double annuallnterestRate;
    private LocalDate dateCreated;

    public Account(){

    }
    public Account(int id,double balance){
        this.id=id;
        this.balance=balance;
    }
    public int getId(int id){
        return id;
    }

    public void setId(){
        this.id=id;
    }

    public double getBlance(double balance){
        return balance;
    }

    public void setBalance(){
        this.balance=balance;
    }

    public double getAnnuallnterestRate(double annuallnterestRate){
        return annuallnterestRate;
    }

    public void setAnnuallnterestRate(){
        this.annuallnterestRate=annuallnterestRate;
    }

    public void getDateCreated(){
        System.out.println("The Account'dateCreated:2020-07-31");
    }

    public double getMonthlyInterestRate(double balance,double annuallnterestRate){
        double monthlyinterestRate;
        monthlyinterestRate=balance*(annuallnterestRate/1200);
        return monthlyinterestRate;
    }
    public boolean withDraw(double withdraw){
        boolean result =true;
        if(withdraw>this.balance||withdraw<0)
            result=false;
        return result;
    }
    public boolean deposit(double deposit){
        boolean result = true;
        if(deposit>20000||deposit<0)
            result=false;
        return result;
    }
}

这道题算是我们对类的认识的开始,对类里面方法的使用,对类的了解就是从这道题开始的

附上类图:

 定义日期类:

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

创建一个日期类,里面写入实现该功能的方法,先判断是否为润年,如果是润年,然后mon_maxnum[2]=29,再看输入日期是否为该月的最后一天,是的话month+1,如果是12月,就year+1,month=1,day=1;

附上类图:

 日期类设计:

参考题目3和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:

public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值
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;
    public DateUtil(DateUtil d){
        this.day=d.getDay();
        this.month=d.getMonth();
        this.year=d.getYear();
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
    }
    public DateUtil(){

    }
    public DateUtil(int year,int month,int day){
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public void setYear(){
        this.year=year;
    }
    public int getYear(){
        return year;
    }
    public void setMonth(){
        this.month=month;
    }
    public int getMonth(){
        return month;
    }
    public void setDay(){
        this.day=day;
    }

    public int getDay() {
        return day;
    }

    public boolean isLeapYear(int year){
        boolean isLeapYear = (year%4==0&&year%100!=0)||year%400==0;
        return isLeapYear;
    }
    public boolean checkInputValidity(){
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
        boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=s[month]&&day>0);
        return checkInputValidity;
    }
    public DateUtil getNextNDays(int n){
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        while(n>365) {
            if (this.isLeapYear(year) && month <= 2) {
                if (month == 2 && day == 29) {
                    day = 1;
                    month = 3;
                    year++;
                n = n - 366;
                }else if(month==2&&day<29){
                    year++;
                    n=n-366;
                }
                else{
                    year++;
                    n=n-366;
                }
                
            } 
            else if (this.isLeapYear(year) && month > 2) {
                year++;
                n = n - 365;
            }
            else if (this.isLeapYear(year + 1) && month > 2) {
                year++;
                n = n - 366;
            }
            else {
                year++;
                n = n - 365;
            }
        }
        while(n+day>s[month]){
            if(this.isLeapYear(year)){
                s[2]=29;
            }
            n=n-s[month];
            month++;
            if(month==13){
                month=1;
                year++;
            }
        }
        day=day+n;
        return this;
    }
    public DateUtil getPreviousNDays(int n){
        int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            s[2]=29;
        else
            s[2]=28;
        while(n>365){
            if(this.isLeapYear(year)&&month>2){
                n=n-366;
                year--;
            }
            else if(this.isLeapYear(year)&&month==2&&day==29){
                day=28;
                year--;
                n=n-366;
            }
            else if(this.isLeapYear(year-1)&&month<3){
                year--;
                n=n-366;
            }
            else{
                n=n-365;
                year--;
            }
        }
         while((day-n)<=0){
            if(this.isLeapYear(year)){
                s[2]=29;
            }
             else{
                 s[2]=28;
             }
            n=n-day;
            month--;
             
            if(month==0){
                month=12;
                year--;
            }
             day=s[month];
        }
        day=day-n;
        return this;
    }
    public int getDaysofDates(DateUtil date){
        int days=0;
        if(equalTwoDates(date)){
            days=0;
            return days;
        }
        else{
            int[] s=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31};
            if(compareDates(date)==true){
                if(this.year==date.year){
                    if(isLeapYear(this.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    if(this.month==date.month){
                        days=days+this.day-date.day;
                    }
                    else{
                        for(int i=date.month+1;i<this.month;i++){
                            days=days+s[i];
                        }
                        days=days+s[date.month]-date.day+this.day;
                    }
                }
                else{
                    for(int i=date.year+1;i<this.year;i++){
                        if(isLeapYear(i)){
                            days=days+366;
                        }
                        else{
                            days=days+365;
                        }
                    }
                    if(isLeapYear(date.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    for(int i=date.month+1;i<=12;i++){
                        days=days+s[i];
                    }
                    days=days+s[date.month]-date.day;
                    if(isLeapYear(this.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    for(int i=1;i<this.month;i++){
                        days=days+s[i];
                    }
                    days=days+this.day;
                }
                return days;
            }
            else{
                if(this.year==date.year){
                    if(isLeapYear(this.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    if(this.month==date.month){
                        days=days+date.day-this.day;
                    }
                    else{
                            for(int i=this.month+1;i<date.month;i++){
                            days=days+s[i];
                        }
                        days=days+s[this.month]-this.day+date.day;
                    }
                }
                else{
                    
                    for(int i=this.year+1;i<date.year;i++){
                        if(isLeapYear(i)){
                            days=days+366;
                        }
                        else{
                            days=days+365;
                        }
                    }
                    if(isLeapYear(this.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    for(int i=this.month+1;i<=12;i++){
                        days=days+s[i];
                    }
                    days=days+s[this.month]-this.day;
                    if(isLeapYear(date.year)){
                        s[2]=29;
                    }
                    else{
                        s[2]=28;
                    }
                    for(int i=1;i<date.month;i++){
                        days=days+s[i];
                    }
                    days=days+date.day;
                }
                return days;
            }
        }
    }
    public boolean compareDates(DateUtil date){
        if(this.year>date.getYear())
            return true;
        else if(this.year==date.getYear()&&this.month>date.getMonth())
            return true;
        else if(this.year==date.getYear()&&this.month== date.getMonth()&&this.day>date.getDay())
            return true;
        return false;
    }
    public boolean equalTwoDates(DateUtil date){
        if(this.year!=date.getYear())
            return false;
        else if(this.day!=date.getDay())
            return false;
        else if(this.month!=date.getMonth())
            return false;
        else
            return true;
    }
    public String showDate(){
        return year + "-" + month + "-" + day;
    }
}

对于这道题,确实是花费了很久,感觉是前两次题目的进阶版,不再只是下一天,变成了下N天,还有前N天,还有两天之间共差了多少天,前两个倒是同一种思路,在求两天之间,倒是遇到了不少麻烦。在求后N天中,先写一个while循环,N>365,就一直进行年之间的增加,如果是润年并且此时日期在2月29号之前,N-366,如果是29号,就把日期换成3月1号,同样N-366。如果下一年是润年,且month>2,那也要N-366,其他情况N-365;到了N<=365后,就可以实现一天一天的增加,到了月的最后一天,就month+1,day=1,如果是12月最后一天+1,那就year+1,month=1,day=1,如此进行下去得出结果。求前N天与此类似。在求两个日期之间的天数,就从年到月到日,慢慢加过去,细说起来有点麻烦,就先比较两个输入日期年份的大小,然后小的那个日期,一年一年增加,如果是润年就加上366,到了月份也是一月的最大天数加上去,然后日就加上自己的,减轻另一个的。

附上类图:

3、踩坑心得:

  这三次作业中,我印象深刻的就是有重复数据,当时想了好久,始终有两个测试点过不了,应该是运行超时的问题,当时一直想不出来解决方法,后来在书上找到Arrays中的sort这个方法,通过使用sort方法,将数据排序后通过一个for循环就解决了。还有就是题目集三的第四题,在那个求两个日期之间的天数,在这个问题的解决中,遇到了很多困难,每次改完代码,运行所给的案例能够正确,但是测试点过不了,就证明所写代码的算法中还存在一定问题,可是当时又一直没有发现,一直更换测试样例都没有找到错误原因,后来仔细阅读自己的算法,确实是发现了其中的问题,但是当时修改很麻烦,当时自己差点就放弃,不想该了,感觉很烦。后来还是说耐着性子,把原来的算法改了,其中开始没有考虑到的原因就是如果该年是润年还需考虑月份是否小于2月,还有下一年是润年,该月份是否大于2月。还有就是在自己写完自己的代码之后,可以去问问同学们他们的算法或者去网上学学其他方法,可能可以很大程度的改善自己的代码。

  还有就是要读清楚题目如下图:

  如该图中,一个能创建带特定id和初始余额的账号的构造方法,一开始我就没有注意到这个东西,在写完代码后发现,我的pta运行时,参数的值没有传入,一直找不到解决的方法,后来还是在用IDEA的debug中发现值没有传入,然后仔细读题,找到了问题所在。

4、改进建议:

  在完成一道题目后不要只想着自己完成了就完事了,还要想着如何去改进自己的代码,如何去简化自己的代码,多去学习别人的思路和想法,而不是只是一味的通过测试点,这样只会让编程变得更加枯燥,还有就是编程的算法是离不开数学能力的,如第一次作业的那道求定积分,其中就需要数学的方法,虽然通过读题也能理解,但是你开始就会,就能节省更多时间,在解决其它题目时,我也要学着多用数学的思维去解决我们所遇到的问题,算法是我们编程的核心,多去思考与发现。

 5、总结:

  我们现在刚开始接触java这门语言,这门语言比起上学期的C语言,我感觉难度一下就上来了,这可能就是程序设计基础与面向对象的区别,一者是我们对计算机语言的入门,而现在的java却是我们迈向编程的关键,其难度相比差之甚远,加上java里面的许多东西都需要我们自己去学习完成,使我们的负担更重,java里面的类和方面,也让我们感觉到java更加贴近我们身边那些程序的设计,与我们未来的工作更加有关联。之前的我总是习惯性的看到题目就打开csdn去上面搜索,那样虽然最后能够解决掉这个问题,但是学到的东西甚少,也缺失自己完成一道道题目的成就感,虽然学习这个过程很累,但是学会每一个东西,都想是学习过程中给我们的奖励和收获,我们要从学习获得收获,取得成就感,那样我们才能一直保持学习的初心,不会半途而废,java这段学习过程很累,感觉事情很多,自己缺失了很多娱乐的时间,但是有收获就是值得的,毕竟现在的我们不在是高中生,我们以后面向的是社会,第一次写博客,有感而发,希望自己能在日后的编程学习中做的越来越好!