grafspee pta1-3作业总结

本文将对pta上1-3次作业进行总结

nchu_22201129_xiahouyuchen

目录

一.前言

二.设计与分析

三.踩坑心得

四.改进建议

五.总结

 

前言

这学期开始,我们通过java开始了面向对象语言的学习,由于我们之前并没有接触面向对象语言,所以从之前的c和python转过来稍微有些吃力,目前就已经写过的3次作业进行总结。

第一次作业:

知识点:简单的循环,判断结构,以及基础的字符串操作;

题量:12道题

难度:中等

 

第二次作业:

 

知识点:类的操作;

 

题量:4道题

 

难度:中等

 

第三次作业:

 

知识点:简单的循环,判断结构,以及基础的字符串操作;

 

题量:12道题

 

难度:

 

设计与分析:

题目集1

.7-9 Prime Numbers

//素数

Your program reads two natural numbers m and n in, and prints out the sum of all prime numbers within [m,n], where 1mn104.

//您的程序读取两个自然数m和n,并输出[m,n]内所有素数的和,其中1⩽m≤n⩽10

 

Input Format:

//输出格式

Two positive whole numbers.

//两个正整数

Output Format:

//输出

A number which is the sum of all the prime numbers within [m, n].

//是[m, n]内所有素数的和。

输入样例

10 100

输出样例

1043

代码如下

 

 1 import java.util.Scanner;
 2 
 3 public class Main
 4 {
 5     public static void main(String[] args){
 6         Scanner in = new Scanner(System.in);
 7         int m = in.nextInt();
 8         int n = in.nextInt();
 9         int sum = 0;
10         int i,j;
11         for ( i = m; i <= n; i++) {
12             for (j = 2; j < i; j++) {
13                 if (i % j == 0) {
14                     break;
15                 }
16             }
17             if (j == i) {
18                 sum += i;
19             }
20         
21     }System.out.println(sum);
22 }}

 

简要分析:本题抛开英文部分,即为一个对素数的判断加上一个对判断出来的素数进行循环相加。其中判断素数的部分使用两个循环来进行判断。

 

题目集2

7-1 长度质量计量单位换算

长度、质量的计量有多重不同的计算体系,有标准的国际单位制:千克与米,也有各个国家自己的计量方法如:磅、英寸;1磅等于0.45359237千克,1英寸等于0.0254米,请编写程序实现国际单位制与英制之间的换算。

输入格式:

两个浮点数,以空格分隔,第一个是质量(以千克为单位)、第二个是长度(以米为单位)。例如:0.45359237 0.0254。

输出格式:

两个浮点数,以空格分隔,第一个是质量(以磅为单位)、第二个是长度(以英寸为单位)。例如:1.0 1.0。

输入样例:

在这里给出一组输入。例如:

0.45359237 0.0254

输出样例:

在这里给出相应的输出。例如:

1.0 1.0

代码样例

 1 import java.util.Scanner;
 2 
 3 public class Main
 4 {
 5     public static void main(String[] args){
 6         Scanner in = new Scanner(System.in);
 7         double weight = in.nextFloat();
 8         double length = in.nextFloat();
 9         double a;
10         double b;
11         a = weight/0.45359237;
12         b = length/0.0254;
13         System.out.printf((float)a+" "+(float)b);  
14         
15     }}

 

 简要分析:本题主要检测为强制转换,如果不强制转换则精度会出问题。

 

7-5 学号识别
 

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

输入格式:

8位数字组成的学号。例如:18011103
注意:输入学号不是8位或者学院编号不是01、02、03、20其中之一,属于非法输入

输出格式:

学号每一项的完整说明。例如:
入学年份:2018年
学院:材料学院
班级:11
学号:03

注意:如非法输入,输出“Wrong Format"

输入样例:

在这里给出一组输入。例如:

18011103
 

输出样例:

在这里给出相应的输出。例如:

入学年份:2018年
学院:材料学院
班级:11
学号:03
 

输入样例1:

在这里给出一组输入。例如:

18013
 

输出样例1:

在这里给出相应的输出。例如:

Wrong Format

代码如下

 1 import java.util.Scanner;
 2 
 3 public class Main
 4 {
 5     public static void main(String[] args){
 6         Scanner in = new Scanner(System.in);
 7         int bianhao = in.nextInt();
 8         int xueyuan;
 9         int xuehao;
10             xuehao = bianhao%100;
11         int banji;
12         banji = (bianhao%10000-xuehao)/100;
13         xueyuan = (bianhao%1000000-bianhao%10000)/10000;
14         if((bianhao/10000000<0.1||bianhao/10000000>=1)&&(xueyuan!=01&&xueyuan!=02&&xueyuan!=03&&xueyuan!=20))
15         {
16             System.out.println("Wrong Format");
17         }
18         else
19         {
20         int nianfen;
21         nianfen = (bianhao%100000000-bianhao%1000000)/1000000;
22         System.out.println("入学年份:"+"20"+nianfen+"年");  
23             switch(xueyuan)
24             {
25                 case 1:
26                     System.out.println("学院:材料学院");
27                     break;
28                 case 2:
29                     System.out.println("学院:机械学院");
30                     break;
31                 case 3:
32                     System.out.println("学院:外语学院");
33                     break;
34                 case 20:
35                     System.out.println("学院:软件学院");
36                     break;
37             }
38             System.out.println("班级:"+banji);
39             if(xuehao<10)
40             {
41                 System.out.println("学号:0"+xuehao);
42             }
43             
44             else
45             {System.out.println("学号:"+xuehao);}
46         }
47     }}

 

 

简要分析:本题主要的难点为如何把一串整体的字符串拆分出来,较为常规的方法为使用哈希以及stringbuffer,但是此方法被老师ban掉了,所以我的思路为使用求余来进行各号码的分开,但是使用求余不可避免的会出现无法舍去后面符号的情况,所以你必须再减去后面的求余数。

 

7-8 判断三角形类型
 

输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:

在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”;
(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;
(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;
(3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;
(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;
(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;
(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

输入样例1:

在这里给出一组输入。例如:

50 50 50.0
 

输出样例1:

在这里给出相应的输出。例如:

Equilateral triangle
 

输入样例2:

在这里给出一组输入。例如:

60.2 60.2 80.56
 

输出样例2:

在这里给出相应的输出。例如:

Isosceles triangle
 

输入样例3:

在这里给出一组输入。例如:

0.5 20.5 80
 

输出样例3:

在这里给出相应的输出。例如:

Wrong Format
代码如下
 1 import java.util.Scanner;
 2 
 3 public class Main
 4 {
 5     public static void main(String[] args){
 6         Scanner in = new Scanner(System.in);
 7         float a = in.nextFloat();
 8         float b = in.nextFloat();
 9         float c = in.nextFloat();
10         if(a<1||a>200||b<1||b>200||c<1||c>200)
11         {
12             System.out.printf("Wrong Format");
13         }
14         else
15         {
16             if(a+b<=c||a+c<=b||b+c<=a)
17             {
18                 System.out.printf("Not a triangle");
19             }
20             else
21             {
22                 if(a==b&&a==c)
23                 {
24                     System.out.printf("Equilateral triangle");
25                 }
26                 else if((a==b&&a*a+b*b-c*c<0.1)||(c==b&&c*c+b*b-a*a<0.1)||(c==a&&c*c+a*a-b*b<0.1))
27                 {
28                     System.out.printf("Isosceles right-angled triangle");
29                 }
30                 else if((a==b&&a*a+b*b!=c*c&&a!=c)||(a==c&&a*a+c*c!=b*b&&a!=b)||(b==c&&b*b+c*c!=a*a&&a!=c))
31                 {
32                     System.out.printf("Isosceles triangle");
33                 }
34                 else if((a!=b&&a*a+b*b==c*c)||(a!=c&&a*a+c*c==b*b)||(b!=c&&b*b+c*c==a*a))
35                 {
36                     System.out.printf("Right-angled triangle");
37                 }
38                 else if((a!=b&&a*a+b*b!=c*c&&a!=c)||(a!=c&&a*a+c*c!=b*b&&a!=b)||(b!=c&&b*b+c*c!=a*a&&a!=c))
39                 {
40                     System.out.printf("General triangle");
41                 }
42 
43             }
44         }
45     }
46 }

 

 

简要分析:本题大体为重复的判断,但是在等腰直角三角形的判断中,会与直角三角形以及等腰三角形的判断重叠,所以需要多重判断来隔开来,这是问题1,其次,还是精度问题,在直角三角形勾股定理的判断中,计算机无法直接接受a*a+b*b=c*c的信息,只能通过a*a+b*b-c*c<0.1的近似值来达到目的。

题目集3

7-3 定义日期类

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

要求:Date类结构如下图所示:

类图.jpg

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

输入样例1:

在这里给出一组输入。例如:

1912 12 25
 

输出样例1:

在这里给出相应的输出。例如:

Next day is:1912-12-26
 

输入样例2:

在这里给出一组输入。例如:

2001 2 30
 

输出样例2:

在这里给出相应的输出。例如:

Date Format is Wrong

代码如下

  1 import java.util.*;
  2 
  3 public class Main
  4 {
  5     public static void main(String[] args){
  6         Scanner in = new Scanner(System.in);
  7         Date a=new Date();
  8         int year = in.nextInt();
  9         int month = in.nextInt();
 10         int day = in.nextInt();
 11         a.Date(year,month,day);
 12         boolean checkTrue=a.checkInputValidity(year,month,day);
 13         if(checkTrue==false)
 14         {
 15             System.out.printf("Date Format is Wrong");
 16         }
 17         else
 18         {
 19             a.nextDate(year,month,day);
 20         }
 21     }
 22 }
 23 class Date{
 24     int year;
 25     int month;
 26     int day;
 27     int [] mon_maxnum1=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
 28     int [] mon_maxnum2=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
 29     public Date(){}
 30     public void Date(int year,int month,int day){
 31         this.year=year;
 32         this.month=month;
 33         this.day=day;
 34     }
 35     public int getYear(int year){
 36         return year;
 37     }
 38     public void setYear(){
 39         this.year=year;
 40     }
 41      public int getMonth(int month){
 42         return month;
 43     }
 44     public void setMonth(){
 45         this.month=month;
 46     }
 47     public int getDay(int day){
 48         return day;
 49     }
 50     public void setDay(){
 51         this.day=day;
 52     }
 53     public boolean isLeapYear(int year)
 54     {
 55             if(year%4==0&&year%100!=0||year%400==0)
 56             {
 57                 return true;
 58             }
 59             else
 60                 return false;
 61     }
 62     public boolean checkInputValidity(int year,int month,int day)//判断输入日期是否合法,返回布尔值
 63     {
 64             boolean check = true;
 65             if(year<1900||year>2000||month<1||month>12||day<1||day>31)
 66             {
 67                 check = false;
 68             }
 69             else if((year%4==0&&year%100!=0||year%400==0)&&month==2&&day>29)
 70             {
 71                 check = false;
 72             }
 73             else if(year%4!=0&&month==2&&day>28)
 74             {
 75                 check = false;
 76             }
 77             else if((month==4||month==6||month==9||month==11)&&day>mon_maxnum1[month])
 78             {
 79                 check = false;
 80             }
 81             return check;
 82     }
 83     public void nextDate(int year,int month,int day)  //求输入日期的下一天
 84     {
 85             if(isLeapYear(year)==true&&month==2&&day==mon_maxnum2[month])
 86             {
 87                 month=month+1;
 88                 day=1;
 89             }
 90             else if(isLeapYear(year)==false&&month==2&&day==mon_maxnum1[month])
 91             {
 92                 month=month+1;
 93                 day=1;
 94             }
 95             else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==mon_maxnum1[month])
 96             {
 97                 month=month+1;
 98                 day=1;
 99             }
100             else if((month==4||month==6||month==9||month==11)&&day==mon_maxnum1[month])
101             {
102                 month=month+1;
103                 day=1;
104             }
105             else if(month==12&&day==31)
106             {
107                 year=year+1;
108                 month=1;
109                 day=1;
110             }
111             else
112             {
113                 day=day+1;
114             }
115             System.out.printf("Next day is:"+year+"-"+month+"-"+day);
116     }
117 }
118     

 

简要分析:本题主要考察类的创建,思路主要就是以题目所给的类图作为参照来写,他的主体主要要实现对闰年的判断以及输入错误的报错,他的判断下一天我主要采用穷举法来实现,及将每个月的最终天数写出来来进行跨月判断,这样思路简单,但是代码的可修改性较差,应该存入数组,或是单独存一个类来处理跨年跨月的特殊情况。

7-4 日期类设计

参考题目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”格式返回日期值
 

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

程序主方法如下:

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);
        }        
    }
}
 

输入格式:

有三种输入方式(以输入的第一个数字划分[1,3]):

  • 1 year month day n //测试输入日期的下n天
  • 2 year month day n //测试输入日期的前n天
  • 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:

  • 当输入有误时,输出格式如下:
    Wrong Format
  • 当第一个数字为1且输入均有效,输出格式如下:
    year1-month1-day1 next n days is:year2-month2-day2
    
     
  • 当第一个数字为2且输入均有效,输出格式如下:
    year1-month1-day1 previous n days is:year2-month2-day2
    
     
  • 当第一个数字为3且输入均有效,输出格式如下:
    The days between year1-month1-day1 and year2-month2-day2 are:值
    
     

输入样例1:

在这里给出一组输入。例如:

3 2014 2 14 2020 6 14
 

输出样例1:

在这里给出相应的输出。例如:

The days between 2014-2-14 and 2020-6-14 are:2312
 

输入样例2:

在这里给出一组输入。例如:

2 1834 2 17 7821
 

输出样例2:

在这里给出相应的输出。例如:

1834-2-17 previous 7821 days is:1812-9-19
 

输入样例3:

在这里给出一组输入。例如:

1 1999 3 28 6543
 

输出样例3:

在这里给出相应的输出。例如:

1999-3-28 next 6543 days is:2017-2-24
 

输入样例4:

在这里给出一组输入。例如:

0 2000 5 12 30
 

输出样例4:

在这里给出相应的输出。例如:

Wrong Format
代码如下
  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11 
 12         if (choice == 1) { // test getNextNDays method
 13             int m = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24 
 25             m = input.nextInt();
 26 
 27             if (m < 0) {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31 
 32             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 33             System.out.println(date.getNextNDays(m).showDate());
 34         } else if (choice == 2) { // test getPreviousNDays method
 35             int n = 0;
 36             year = Integer.parseInt(input.next());
 37             month = Integer.parseInt(input.next());
 38             day = Integer.parseInt(input.next());
 39 
 40             DateUtil date = new DateUtil(year, month, day);
 41 
 42             if (!date.checkInputValidity()) {
 43                 System.out.println("Wrong Format");
 44                 System.exit(0);
 45             }
 46 
 47             n = input.nextInt();
 48 
 49             if (n < 0) {
 50                 System.out.println("Wrong Format");
 51                 System.exit(0);
 52             }
 53 
 54             System.out.print(
 55                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
 56             System.out.println(date.getPreviousNDays(n).showDate());
 57         } else if (choice == 3) {    //test getDaysofDates method
 58             year = Integer.parseInt(input.next());
 59             month = Integer.parseInt(input.next());
 60             day = Integer.parseInt(input.next());
 61 
 62             int anotherYear = Integer.parseInt(input.next());
 63             int anotherMonth = Integer.parseInt(input.next());
 64             int anotherDay = Integer.parseInt(input.next());
 65 
 66             DateUtil fromDate = new DateUtil(year, month, day);
 67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 68 
 69             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 70                 System.out.println("The days between " + fromDate.showDate() + 
 71                         " and " + toDate.showDate() + " are:"
 72                         + fromDate.getDaysofDates(toDate));
 73             } else {
 74                 System.out.println("Wrong Format");
 75                 System.exit(0);
 76             }
 77         }
 78         else{
 79             System.out.println("Wrong Format");
 80             System.exit(0);
 81         }        
 82     }
 83 }
 84 
 85 
 86 class DateUtil{
 87     private int year;
 88     private int month;
 89     private int day;
 90     int [] mon_maxnum1=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
 91     int [] mon_maxnum2=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
 92     int [] year1=new int[600];
 93 
 94     public DateUtil(){}
 95     public DateUtil(int year,int month,int day){
 96         this.year=year;
 97         this.month=month;
 98         this.day=day;
 99     }
100     public int getYear(){
101         return year;
102     }
103     public void setYear(int year){
104         this.year=year;
105     }
106     public int getMonth(){
107         return month;
108     }
109     public void setMonth(int month){
110         this.month=month;
111     }
112     public int getDay(){
113         return day;
114     }
115     public void setDay(int day){
116         this.day=day;
117     }
118 
119     public boolean isLeapYear(int year)
120     {
121         if(year%4==0&&year%100!=0||year%400==0)
122         {
123             return true;
124         }
125         else
126             return false;
127     }
128     public boolean checkInputValidity()//判断输入日期是否合法,返回布尔值
129     {
130         boolean check = true;
131         if(year<1900||year>2000||month<1||month>12||day<1||day>31)
132         {
133             check = false;
134         }
135         else if((year%4==0&&year%100!=0||year%400==0)&&month==2&&day>29)
136         {
137             check = false;
138         }
139         else if(year%4!=0&&month==2&&day>28)
140         {
141             check = false;
142         }
143         else if((month==4||month==6||month==9||month==11)&&day>mon_maxnum1[month])
144         {
145             check = false;
146         }
147         return check;
148     }
149     public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
150     {
151         int i;
152         int sum=0;
153         int t=0;
154         int k=0;
155         for(i=0;i<400;i++)
156         {
157             if(this.isLeapYear(year)==true)
158             {
159                 year1[i] = 366;
160             }
161             else if(this.isLeapYear(year)==false)
162             {
163                 year1[i] = 365;
164             }
165         }
166         if(this.isLeapYear(year)==true)
167         {
168             t = mon_maxnum2[month]-day;
169             sum = t;
170             for(i=month+1;i<=12;i++)
171             {
172                 sum = mon_maxnum2[i] + sum;
173             }
174             t=sum;//剩余天数
175             if(sum<n)
176             {
177                 for(i=year-1820;sum<=n;i++)
178                 {
179                     sum = sum + year1[i];
180                 }
181                 t = n - sum;
182                 k=i+1820;//year
183                 year = k;
184                 if(this.isLeapYear(k)==true)
185                 {
186                     if(t<mon_maxnum2[1])
187                     {
188                         day = t;
189                     }
190                     else
191                     {
192                         sum=0;
193                         for(i=1;sum<n;i++)
194                         {
195                             sum = sum+mon_maxnum2[i];
196                         }
197             //i=month
198                         i = month;
199                         day=n-sum;
200                     }
201                 }
202             }
203         }
204         else if(this.isLeapYear(k)==false){
205             if(t<mon_maxnum1[1]){
206                 day = t;
207             }
208             else{
209                 sum=0;
210                 for(i=1;sum<n;i++){
211                     sum = sum+mon_maxnum1[i];
212                 }
213                 month = i;
214             //i=month
215                 day = n -sum;
216             }
217             if(n>=sum){
218                 sum = mon_maxnum2[month]-day;
219                 for(i=month+1;sum<=n;i++)
220                 {
221                     sum = sum + mon_maxnum2[i];
222                     day = n - sum;
223                 }
224                 if(this.isLeapYear(year)==false){
225                     t = mon_maxnum1[month]-day;
226                     sum = t;
227                     for(i=month+1;i<=12;i++){
228                         sum = mon_maxnum1[i] + sum;
229                     }
230                     t=sum;//剩余天数
231                 }
232                 if(sum<n){
233                     for(i=year-1820;sum<=n;i++){
234                         sum = sum + year1[i];
235                     }
236                     t = n - sum;
237                     k=i+1820;//year
238                     year = k;
239                     if(this.isLeapYear(k)==true)
240                     {
241                         if(t<mon_maxnum2[1]){
242                             day = t;
243                         }
244                         else{
245                             sum=0;
246                             for(i=1;sum<n;i++)
247                             {
248                                 sum = sum+mon_maxnum2[i];
249                             }
250                             month = i;
251             //i=month
252                             day=n-sum;
253                         }
254                     }
255                     if(this.isLeapYear(k)==false)
256                     {
257                         if(t<mon_maxnum1[1])
258                         {
259                             day = t;
260                         }
261                         else
262                         {
263                             sum=0;
264                             for(i=1;sum<n;i++)
265                             {
266                                 sum = sum+mon_maxnum1[i];
267                             }
268                             month = i;
269             //i=month
270                             day = n -sum;
271                         }
272                     }
273                 }
274                 else if(n>=sum)
275                 {
276                     sum = mon_maxnum2[month]-day;
277                     for(i=month+1;sum<=n;i++)
278                     {
279                         sum = sum + mon_maxnum2[i];
280                         month = i;
281                         day = n - sum;
282                     }
283                 }
284             }
285         }
286         return this;
287     }
288     public DateUtil getPreviousNDays(int n){
289         return this;
290     }
291     public boolean compareDates(DateUtil date){
292         return false;
293     }
294     public boolean equalTwoDates(DateUtil date){
295         return false;
296     }
297     public int getDaysofDates(DateUtil date){
298         int allday=0;
299         return allday;
300     }
301     public String showDate()
302     {
303         String a=year+"-"+month+"-"+day;
304         return a;
305     }
306 }//写不出来

事先申明:本题没写出来

失败心得:1,类方面的使用,不够熟练,前面题目事先给出的调用类没有看懂,其次return以及this的指向并不是很清楚,到时就是将代码修复到可运行状态都花费了很久的时间,修复过程中的主要问题在286行return this里以及301最后一个返回类的输出玩不明白

2,思路的问题:该题在书写时目前只写了第一种情况,在算下n天时,我的想法是先将常年和闰年的年份对应的元素存入一个数组以及再将二者对应的月份分别存入相应的数组,再通过循环算出是否跨年,如果未跨年则再用循环判断到指定的月份,跨年后再用循环判断跨几年,再用循环判断具体过到哪天,但是这样想象起来很简洁,但是实际上操作起来代码十分长(打听了一下同学差不多都是300行写完,我一个功能就有300行了)

踩坑心得

1,看到题直接上手,并没有认真看题,导致上来就编译错误或是答案错误
错误示例题目集1 7-9

 题目原意为求素数,我直接省略掉了,导致答案错误

 改完后(于11行增加了素数判断)

 

 

 

2,逻辑较为混乱,题目的基础思路很杂错误示例:题目集2 7-5以及题目集3 7-47-5

修改后(错误原因:编号较多导致求余分割出现了逻辑问题,具体在13行)

 

 

 7-5

 1 Main.java:18: error: constructor DateUtil in class DateUtil cannot be applied to given types;
 2             DateUtil date = new DateUtil(year, month, day);
 3                             ^
 4   required: no arguments
 5   found: int,int,int
 6   reason: actual and formal argument lists differ in length
 7 Main.java:32: error: method getYear in class DateUtil cannot be applied to given types;
 8             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
 9                                  ^
10   required: int
11   found: no arguments
12   reason: actual and formal argument lists differ in length
13 Main.java:32: error: method getMonth in class DateUtil cannot be applied to given types;
14             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
15                                                         ^
16   required: int
17   found: no arguments
18   reason: actual and formal argument lists differ in length
19 Main.java:32: error: method getDay in class DateUtil cannot be applied to given types;
20             System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
21                                                                                 ^
22   required: int
23   found: no arguments
24   reason: actual and formal argument lists differ in length
25 Main.java:33: error: method getNextNDays in class DateUtil cannot be applied to given types;
26             System.out.println(date.getNextNDays(m,year,month,day).showDate());
27                                    ^
28   required: int
29   found: int,int,int,int
30   reason: actual and formal argument lists differ in length
31 Main.java:40: error: constructor DateUtil in class DateUtil cannot be applied to given types;
32             DateUtil date = new DateUtil(year, month, day);
33                             ^
34   required: no arguments
35   found: int,int,int
36   reason: actual and formal argument lists differ in length
37 Main.java:55: error: method getYear in class DateUtil cannot be applied to given types;
38                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
39                         ^
40   required: int
41   found: no arguments
42   reason: actual and formal argument lists differ in length
43 Main.java:55: error: method getMonth in class DateUtil cannot be applied to given types;
44                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
45                                                ^
46   required: int
47   found: no arguments
48   reason: actual and formal argument lists differ in length
49 Main.java:55: error: method getDay in class DateUtil cannot be applied to given types;
50                     date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
51                                                                        ^
52   required: int
53   found: no arguments
54   reason: actual and formal argument lists differ in length
55 Main.java:56: error: cannot find symbol
56             System.out.println(date.getPreviousNDays(n).showDate());
57                                    ^
58   symbol:   method getPreviousNDays(int)
59   location: variable date of type DateUtil
60 Main.java:66: error: constructor DateUtil in class DateUtil cannot be applied to given types;
61             DateUtil fromDate = new DateUtil(year, month, day);
62                                 ^
63   required: no arguments
64   found: int,int,int
65   reason: actual and formal argument lists differ in length
66 Main.java:67: error: constructor DateUtil in class DateUtil cannot be applied to given types;
67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
68                               ^
69   required: no arguments
70   found: int,int,int
71   reason: actual and formal argument lists differ in length
72 Main.java:72: error: cannot find symbol
73                         + fromDate.getDaysofDates(toDate));
74                                   ^
75   symbol:   method getDaysofDates(DateUtil)
76   location: variable fromDate of type DateUtil
77 Main.java:292: error: missing method body, or declare abstract
78     public String showDate();
79                   ^
80 Main.java:295: error: return outside method
81         return a;
82         ^
83 15 errors

这是7-4的最终报错,当时就在这些报错修改中,就连如何修改的思路都没有,将报错提示在网上检索也收获较少,最后是在大牛的协助下才将报错修改了

修改后

 

 但最终也只过了报错提示,估计是下n天有逻辑错误

由于题级结束了,所以无法看到运行结果(没有进去)

 

3,代码量少,在书写以及调试下容易当无头苍蝇,在报错时不知道如何修改
同2中7-4的问题
 

 

改进建议

1,在书写代码时尽量将各功能封装在相应函数中,避免因全写在主函数中造成的可读性低,以及低调试性

2,在书写前不要去网上借鉴相应的代码,尽管按自己的思路可能会写的很恶心,但是写代码还是需要时间来思考,以及需要时间来踩坑,增加对代码的理解,以及对代码的敏感度

3,多去网上学习好的代码风格,来增加自己代码的可调试性

 

总结

此三次题目集让我对java中的循环,判断,字符串的处理,函数(类),有了基本的了解,目前我对自己的想法是,字符串的操作以及类的使用需要大幅度增强,这两个知识点还是处于一看就会,但是自己上手就出各种毛病;在课程教授方面,我个人希望老师放慢些节奏,因为对于我一个没有往前学很多的小白,很多知识点还是需要大量时间点去了解与消化,对于作业方面,希望老师多给点完成时间,因为平常课时加上作业都很多,每一次pta作业都需要耗费大把时间在上面;课程教授方面,我希望能更多看到老师随堂演示代码的书写

posted @ 2023-03-24 08:57  grafspee  阅读(56)  评论(0)    收藏  举报