第一次博客总结


                 目录

1. 前三个题目集的知识归纳及相关情况

2.对重要题目进行设计分析并总结心得

3.对程序中的问题深入分析,优化解决方法

4.全局的总结

 

 

一. 前三个题目集的知识归纳及相关情况

( 1 )第一个题目集

         1.  知识点包含对非法输入的判断,数据类型的使用(例如:int,float,double....),对数组和字符串的相关操作。

 

         2. 总共包含九个题目,前五道题目考察基本的语法使用,后四道题难度略微提升,总体难度偏易。

 

         3. 注意double数据计算时存在误差,需要将误差error加入if判断语句中,以免造成答案的测试有偏差。

 ( 2 )  第二个题目集

         1. 主要是关于对字符串的处理和提取。

 

         2.  总共包含三个题目,第二题略微复杂。

             

        3. 第二题注意理解奇偶校验位和奇校验的定义。

 

( 3 )第三个题目集

       1. 三个题目是层层递进的,第一个点的类在线的类中实现,第三个三角形的类与点和线的类有关联

   

       2. 前两个题目的类的设计需要做到具体,以便第三个题应用前两个类

 

      3. 首次使用类,并定义类中的方法与属性

 

二. 对重要题目进行设计分析并总结心得

 ( 1 )第二个题目集的第二题

        

复制代码

import java.util.*;
public class Test {

public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
s=sc.nextLine();
int amount=0;
int sum=0;

for(int i=0;i<s.length()||i+10<=s.length();i++){
if(s.charAt(i)=='1'){
amount++;
}
}
if(amount==s.length()||s.length()<11){
System.out.print("null data");
}
for(int i=0;i<s.length();){
if(s.charAt(i)=='0'){
int k=0;
for (int j = i + 1; j < i + 9; j++) {
if (s.charAt(j) == '1' )
k++;
}
if(s.charAt(i + 9) == '1'){
k++;
}
if (k % 2 != 0&&s.charAt(i + 10) == '1') {
sum++;
System.out.println(sum + ":" + s.substring(i + 1, i + 9));
}
if (k % 2 == 0&&s.charAt(i + 10) == '1'){
sum++;
System.out.println(sum + ":" + "parity check error");
}
if(s.charAt(i + 10) != '1'){
sum++;
System.out.println(sum + ":" + "validate error");
}

i+=11;
}
else{
i++;
}
}
}

}

复制代码

 

 

 

 

 

 

 

 分析:这道题整体难度一般,但是要理解数据流和奇偶校验位的关系,以及奇校验的判断方式即可,但是判断格式输入设计不合理。

(2)第三个题目集

1.题目1

 代码如下

复制代码
import ja
va.util.*; public class Test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); double sum; String[] str = s.split("\\s+"); String[] s1=str[0].split(","); int number=str.length; String[] s2=str[1].split(","); int douha=0; if(str[0].matches("\\,")&&str[1].matches("\\,")) { douha=-1; } else { douha=1; } if(!s.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){1,}")) { System.out.print("Wrong Format"); } else { if(number>=3) { System.out.print("wrong number of points"); } else { double x1 = ParseDouble(s1[0]); double y1 = ParseDouble(s1[1]); double x2 = ParseDouble(s2[0]); double y2 = ParseDouble(s2[1]); sum = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); System.out.print(sum); } } } public static Double ParseDouble(String s){ if(s.matches("^[-+]?[0-9]*\\.?[0-9]+$")){ return Double.valueOf(s); } else{ return null; } } }
复制代码

 出现问题:

 

 将

       String[] str = s.split("\\s+");
        String[] s1=str[0].split(",");
改为
String str=s.replace(","," ");
String[]s1=str.split("\\s+");

 

 

 

 

2.题目2

代码如下

复制代码
import java.util.*;
public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int startNumber = 0;
        int k = 0;//判断是否符合坐标输入模式
        int m = 0;//判断符合模式并且坐标数量正确
        String s = sc.nextLine();
        if (s.charAt(0) >= '1' && s.charAt(0) <= '5' && s.charAt(1) == ':') {
            startNumber = Integer.parseInt(String.valueOf(s.charAt(0)));
            String news = s.substring(2);
            if ((news.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){1,}"))) {
                k = -1;
            }
        }
        if (k == 0) {
            System.out.print("Wrong Format");
            m++;
        } else {
            String news = s.substring(2);
            switch (startNumber) {
                case 1:
                    if ((!news.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){2}"))) {
                        System.out.print("wrong number of points");
                        m++;
                    }
                    break;
                case 2:
                case 3:
                    if ((!news.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){3}"))) {
                        System.out.print("wrong number of points");
                        m++;
                    }
                    break;
                case 4:
                case 5:
                    if ((!news.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){4}"))) {
                        System.out.print("wrong number of points");
                        m++;
                    }
                    break;
            }
        }

        if (startNumber == 1 && m == 0) {
            String news = s.substring(2);
            String s1 = news.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            if (str1[0].equals(str1[2])) {
                if (str1[1].equals(str1[3])) {
                    System.out.print("points coincide");
                } else {
                    System.out.print("Slope does not exist");
                }
            }
            else {
                double k1;
                double[] point = new double[4];
                for (int i = 0; i < str1.length; i++) {
                    point[i] = Double.parseDouble(str1[i]);
                }
                k1=(point[1]-point[3])/(point[0]-point[2]);
                System.out.print(k1);
            }
        }
        if (startNumber == 2 && m == 0) {
            String news = s.substring(2);
            String s1 = news.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");

            if ((str1[0].equals(str1[2])) && (str1[1].equals(str1[3]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[4])) && (str1[1].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[4])) && (str1[3].equals(str1[5]))) {
                System.out.print("points coincide");
            } else {
                double h;
                double a;
                double b;
                double[] point = new double[6];
                for (int i = 0; i < str1.length; i++) {
                    point[i] = Double.parseDouble(str1[i]);
                }
                if(point[3] - point[5]==0){
                    h=point[1];
                }
                else if((point[2] - point[4])==0) {
                    h=point[0];
                }
                else{
                a = (point[3] - point[5]) / (point[2] - point[4]);
                b = a * point[2] - point[3];
                h = (Math.abs(point[0] * a * (-1.0) + point[1] - b)) / Math.sqrt(a * a + b * b);
                }
                System.out.print(h);
            }
        }
        if (startNumber == 3 && m == 0) {

            String news = s.substring(2);
            String s1 = news.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            if ((str1[0].equals(str1[2])) && (str1[1].equals(str1[3]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[4])) && (str1[1].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[4])) && (str1[3].equals(str1[5]))) {
                System.out.print("points coincide");
            } else {
                double[] point = new double[6];
                for (int i = 0; i < str1.length; i++) {
                    point[i] = Double.parseDouble(str1[i]);
                }
                double x1;
                double y1;
                double x2;
                double y2;
                x1 = point[0] - point[2];
                y1 = point[1] - point[3];
                x2 = point[2] - point[4];
                y2 = point[3] - point[5];
                if (x1 * y2 == x2 * y1) {
                    System.out.print("true");
                } else {
                    System.out.print("false");
                }
            }
        }
        if (startNumber == 4 && m == 0) {
            String news = s.substring(2);
            String s1 = news.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            if ((str1[0].equals(str1[2])) && (str1[1].equals(str1[3]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[4])) && (str1[1].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[6])) && (str1[1].equals(str1[7]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[4])) && (str1[3].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[6])) && (str1[3].equals(str1[7]))) {
                System.out.print("points coincide");
            } else if ((str1[4].equals(str1[6])) && (str1[5].equals(str1[7]))) {
                System.out.print("points coincide");
            } else {
                double k1;
                double k2;
                double[] point = new double[8];
                for (int i = 0; i < str1.length; i++) {
                    point[i] = Double.parseDouble(str1[i]);
                }
                k1 = (point[3] - point[1]) / (point[2] - point[0]);
                k2 = (point[7] - point[5]) / (point[6] - point[4]);
                if (k1 == k2) {
                    System.out.print("true");
                } else {
                    System.out.print("false");
                }
            }

        }
        if (startNumber == 5 && m == 0) {
            String news = s.substring(2);
            String s1 = news.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            if ((str1[0].equals(str1[2])) && (str1[1].equals(str1[3]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[4])) && (str1[1].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[0].equals(str1[6])) && (str1[1].equals(str1[7]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[4])) && (str1[3].equals(str1[5]))) {
                System.out.print("points coincide");
            } else if ((str1[2].equals(str1[6])) && (str1[3].equals(str1[7]))) {
                System.out.print("points coincide");
            } else if ((str1[4].equals(str1[6])) && (str1[5].equals(str1[7]))) {
                System.out.print("points coincide");
            } else {
                double k1;
                double k2;
                double[] point = new double[8];
                for (int i = 0; i < str1.length; i++) {
                    point[i] = Double.parseDouble(str1[i]);
                }
                k1 = (point[3] - point[1]) / (point[2] - point[0]);
                k2 = (point[7] - point[5]) / (point[6] - point[4]);
                if (k1 == k2) {
                    System.out.print("is parallel lines,have no intersection point");
                } else {
                    double a1, a2, b1, b2;
                    double x, y;
                    if((point[0] - point[2])==0){
                        x=point[0];
                        a2 = (point[5] - point[7]) / (point[4] - point[6]);
                        b2 = point[5] - a2 * point[4];
                        y=a2*x+b2;
                        System.out.print(x + "," + y + " true");
                    }
                    else if((point[4] - point[6])==0){
                        x=point[4];
                        a1 = (point[1] - point[3]) / (point[0] - point[2]);
                        b1 = point[1] - a1 * point[0];
                        y=a1*x+b1;
                        System.out.print(x + "," + y + " true");
                    }
                    else{
                    a1 = (point[1] - point[3]) / (point[0] - point[2]);
                    b1 = point[1] - a1 * point[0];
                    a2 = (point[5] - point[7]) / (point[4] - point[6]);
                    b2 = point[5] - a2 * point[4];
                    x = (b2 - b1) / (a1 - a2);
                    y = a1 * x + b1;
                        System.out.print(x + "," + y + " true");
                    }
                }
            }
        }
    }
}
复制代码

分析

采用个switch语句将四个不同的情况总和在一个函数里,四种情况分别用if,else语句嵌套,复杂度高

 

 

 

3.题目3

代码如下

复制代码
import java.text.DecimalFormat;
import java.util.*;
public class Test3{
    

    public static void main(String[] args) {
        
        
          Scanner sc = new Scanner(System.in);

          String s = sc.nextLine();
          Point p0=new Point();
         int m=p0.pointAmount(s);
         if(m==0) {
             System.out.print("Wrong Format");
         }
         else {
             int amount=Integer.valueOf(s.charAt(0));
            String str=s.substring(2);
              String s1 = str.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            double[] d=new double [m];
            switch(amount) {
              case 1:
                 if(m==3) {
                     Point p1=new Point();
                     Point p2=new Point();
                     Point p3=new Point();
                    for(int i=0;i<str1.length;i++) {
                        d[i]=Double.valueOf(str1[i]);
                    }
                    p1.x=d[0];
                    p1.y=d[1];
                    p2.x=d[2];
                    p2.y=d[3];
                    p3.x=d[4];
                    p3.y=d[5];
                    double dis1=distance( p1,p2);
                    double dis2=distance(p1,p3);
                    double dis3=distance(p2,p3);
                    if(isTriangle(dis1,dis2,dis3)==0.0) {
                        System.out.print("data error");
                        break;
                    }
                    else {
                     if(dis1==dis2&&dis2==dis3) {
                        System.out.print("false true");
                        break;
                     }
                     else if(dis1==dis2||dis1==dis3||dis2==dis3) {
                        System.out.print("true false");
                        break;
                      }
                     else {
                        System.out.print("false false");
                        break;
                     }
                    }
                 }
                 else {
                     System.out.print("wrong number of points");
                     break;
                 }
              
              case 2:
                  if(m==3) {
                         Point p1=new Point();
                         Point p2=new Point();
                         Point p3=new Point();
                        for(int i=0;i<str1.length;i++) {
                            d[i]=Double.valueOf(str1[i]);
                        }
                        p1.x=d[0];
                        p1.y=d[1];
                        p2.x=d[2];
                        p2.y=d[3];
                        p3.x=d[4];
                        p3.y=d[5];
                        double dis1=distance( p1,p2);
                        double dis2=distance(p1,p3);
                        double dis3=distance(p2,p3);
                      if(isTriangle(dis1,dis2,dis3)==0.0) {
                          System.out.print("data error");
                      }
                      else {
                          DecimalFormat df = new DecimalFormat("0.0#####");
                          double perimeter=dis1+dis2+dis3;
                          double h,a,b;
                          if(d[3] - d[5]==0){
                                h=d[1];
                            }
                            else if((d[2] - d[4])==0) {
                                h=d[0];
                            }
                            else{
                            a = (d[3] - d[5]) / (d[2] - d[4]);
                            b = a * d[2] - d[3];
                            h = (Math.abs(d[0] * a * (-1.0) + d[1] - b)) / Math.sqrt(a * a + b * b);
                            }
                          
                          System.out.print(df.format(perimeter)+" "+df.format(h*dis3*1/2)+" "+df.format((p1.x+p2.x+p3.x)/3)+","+df.format((p1.y+p2.y+p3.y)/3));
                      }
                     }
                     else {
                         System.out.print("wrong number of points");
                     }
              
              case 3:
                  if(m==3) {
                      Point p1=new Point();
                         Point p2=new Point();
                         Point p3=new Point();
                        for(int i=0;i<str1.length;i++) {
                            d[i]=Double.valueOf(str1[i]);
                        }
                        p1.x=d[0];
                        p1.y=d[1];
                        p2.x=d[2];
                        p2.y=d[3];
                        p3.x=d[4];
                        p3.y=d[5];
                        double dis1=distance( p1,p2);
                        double dis2=distance(p1,p3);
                        double dis3=distance(p2,p3);
                      if(isTriangle(dis1,dis2,dis3)==0.0) {
                          System.out.print("data error");
                      }
                      else {
                          int k=isangle( dis1, dis2, dis3);
                          if(k==0) {
                              System.out.print("true false false");
                          }
                          else if(k==1) {
                              System.out.print("false true false");
                          }
                          else {
                              System.out.print("false false true");
                          }
                      }
                      
                  }
                  else {
                      System.out.print("wrong number of points");
                  }
              case 4:
                  
              case 5:
                 
             }
             
         }
       }
 private static int isangle(double d1, double d2, double d3) {
     if(Math.pow(d1, 2)+Math.pow(d2, 2)>Math.pow(d3, 2)) {
         return 0;
     }
     else if(Math.pow(d1, 2)+Math.pow(d3, 2)>Math.pow(d2, 2)){
         return 0;
     }
     else if((Math.pow(d3, 2)+Math.pow(d2, 2))>Math.pow(d1, 2)) {
         return 0;
     }
     else if(Math.pow(d1, 2)+Math.pow(d2, 2)==Math.pow(d3, 2)) {
         return 1;
     }
     else if(Math.pow(d1, 2)+Math.pow(d3, 2)==Math.pow(d2, 2)) {
         return 1;
     }
     else if(Math.pow(d2, 2)+Math.pow(d3, 2)==Math.pow(d1, 2)) {
         return 1;
     }
     else {
         return -1;
     }
 }

 private static double isTriangle(double d1, double d2, double d3) {
     if(d1+d2>d3||d1+d3>d2||d2+d3>d1) {
            return 1.0;
        }
        else {
            return 0.0;
        }
    }


private static double distance(Point point1,Point point2) {
    double distance;
     distance=Math.sqrt((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y));
     return distance;
    }
 
public class Point{
     

    
   private  double x;
   private double y;
     
  
    public int pointformate(String s){//判断格式是否正确,并返回一个判断数值。
        if(s.charAt(0)>='1'&&s.charAt(0)<='5'&&s.charAt(1)==':') {
            String str=s.substring(2);
            if(str.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){1,}")) {
                return 1;
            }
            else {
                return 0;
            }
        }
        else {
            return 0;
        }
    }
    public  int pointAmount(String s) {//返回坐标点的数量
        if(pointformate(s)==1) {
            String str=s.substring(2);
            String s1 = str.replaceAll("\\,", " ");
            String[] str1 = s1.split("\\s+");
            return str1.length/2;
        }
        else {
            return 0;
        }
    }
   
 
  }
 }
复制代码
分析:
这题复杂度高,依靠前两题的内容支持,因为我没有用类去解决前两个点和线的问题,所以第三题没有做完整。

 

 三. 对程序中的问题深入分析,优化解决方法

  第二次习题第二题:

    主要的改进点:数据流中的字符串的部分提取,以及奇偶校验的实现

复制代码
加入k用来计算数据流1的个数


if (k % 2 == 0&&s.charAt(i + 10) == '1'){ sum++; System.out.println(sum + ":" + "parity check error"); }
错误结果:

111101110101111111101111111101
1: 11101011

2:11111111

修改后的结果:

 

 

复制代码

 心得:首先设置大概的结构框架,在进行测试点测试时注意代码是否符合设定的想法进行。

 

 

第三次习题第一题

if(!s.matches("([-+]?[0-9]*\\.?[0-9]+[\\,][-+]?[0-9]*\\.?[0-9]+[\\s+]?){1,}")) {
            System.out.print("Wrong Format");

        }

 

利用正则表达式简化输入格式检查部分的测试点,学会正确使用正则表达式对各种格式问题有很大的帮助

 


 按照题意在这种输入应该是wrong format,但是输出结果与题意不符

 主要原因是因为在这种格式判断下容易忽略坐标每个点的格式,03之类的数据属于错误格式的一种特殊情况

但是它又满足if语句力的判断条件,所以不能输出wrong format

解决方法 :设置point类,设置属性坐标,设置方法判断point,设置方法储存坐标,以此可以避免以上的特殊测试点。

 

 

 第三次习题第二题:

    

复制代码
                double h;
                double a;
                double b;
                double[] point = new double[6];
                a = (point[3] - point[5]) / (point[2] - point[4]);
                b = a * point[2] - point[3];
                h = (Math.abs(point[0] * a * (-1.0) + point[1] - b)) / 
                Math.sqrt(a * a + b * b);
                System.out.print(h);
复制代码

 

计算点到直线的距离时,如果直线的斜率为0,此时(point[2] - point[4])作为分母为0,程序崩溃,无数据输出

 

 增加考虑斜率为0 的情况,计算h

复制代码
for (int i = 0; i < str1.length; i++) {
    point[i] = Double.parseDouble(str1[i]);
}
if(point[3] - point[5]==0){
    h=point[1];
}
else if((point[2] - point[4])==0) {
    h=point[0];
}
else{
a = (point[3] - point[5]) / (point[2] - point[4]);
b = a * point[2] - point[3];
h = (Math.abs(point[0] * a * (-1.0) + point[1] - b)) / Math.sqrt(a * a + b * b);
}
System.out.print(h);
复制代码

 

 

 

 心得:需要考虑各种情况的测试点,尤其是特殊情况的测试点不能忽略。


第三次习题第三题:
    Point p0=new Point();

 


 point类中没有对应关于属性x,y的入口,在point类中加入以下代码、

复制代码
public void set(double a,double b){
     this.x=a;
     this.y=b;
}
public double getx(){
   return this.x;
   }
public double gety(){
   return this.y;
   }
复制代码

 

结果如下

 


心得: 此外第三题结合了第一题和第二题的错误测试点,只需要主要前两次作业的错误测试点

将前两题的错误测试点放进第三题中进行测试即可解决未通过的测试点。

 

 

四. 全局的总结

1.学习了面对对象的思维,初步认识了类的概念和怎么定义类。

2.学习了奇偶校验位的含意和奇校验,偶校验的定义

3.了借并学习了正则表达式,利用正则表达式简化格式问题

4.对测试点的考虑不周全,应该先找到测试点,再根据找到的测试点去设计相应的类和方法

5.对面对对象的思维还不熟悉,依旧以面对过程的思维去写程序,导致程序结构混乱,程序内容极度繁琐,可读性极差。


posted @   无心的梦黎  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示