ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BLOG-2

2022-05-09 13:00:19  阅读:170  来源: 互联网

标签:String Point System BLOG point1 point2 public


1)前言:

     题目集四前言:

     题目集一共有3个题目,第一题难度较简单,相对于来说应该是过渡题,考察的主要是正则运算,对字符串中数字的识别以及对数字的加减。第二题题目较难,紧接题目集三的几道题,难度增强,分析点增多,该题考察的重点主要是合法字符的判断读取以及之后根据题目要求来对数据分析计算,同样与使用到了正则运算符,并运用了.replace(" " " ")和.split(" ")和Double.parseDouble(]);将目标字符串中不需要的字符去除后进行分割,并将切割后的字符串读取转化为合法数字然后进行计算。第三题难度中等,开始大范围使用类来简化代码,使代码更加简单清晰,便于后期的删改检查,主要问题是刚开始进行类的使用时不是很熟练导致耗时过长。

     期中考试题目集前言:

     题目集三共有三个题目,整体难度逐渐变难,相对于来说题目都是偏向于基础题。第一题难度较易,简单地使用类来设计代码,只需要按照题目要求来对类名进行定义,考点相对于来说是基础问题。第二题同第一题类似,只是在原有的基础上多加了一个类来定义题目要求的颜色,难度相对于来说也比较简单。第三题

 

     三次题目集的题目的难度从易到难,但考察的知识点较多,大多都与字符串的截取有关,逐步向对象转变。

     三次题目集的得分情况:

      题目集四:42分

      期中考试:68分

 (2)设计与分析

pta4 7-1(源代码如下)

import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(true){
            String line=in.nextLine();
            if(line.equals("end"))
                break;
            String[] split=line.split("\\D+");
            int i;
            long sum=0;
            for(i=0;i<split.length;i++){
                if(!split[i].equals("")){
                    double num=Double.parseDouble(split[i]);//字符串强制转换成double
                    sum+=num;
                }
            }
            System.out.println(sum);
        }
    }
    
}

此题是将字符串中数字提取出来,所运用的正则表达式

String[] split=line.split("\\D+");
能够将数字完整分割,最后在将字符串强制转化为double型,最后再进行计算,按要求逐行输出计算结果。

pta4 7-2(源代码如下)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String z = input.nextLine();
        z=z.replace(","," ");
        z=z.replace(":"," ");
        double x1=0,x2=0,x3=0,x4=0,y1=0,y2=0,y3=0,y4=0;
        String k[]=z.split(" ");
        char s=z.charAt(0);
        if(s == '1') 
        {
               
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                double a1=0;
                double a2=0;
                double a3=0;
                a1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
                a2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                a3 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
                if(((a1==a2)&&(a1!=a3))||((a1==a3)&&(a1!=a2))||((a2==a3)&&(a2!=a1)))
                    {
                      System.out.println("true ");
                    }
                if(((a1==a2)&&(a2==a3)))
                    {
                      System.out.println("true");
                    }
        }
              //  else
               //  {
              //      System.out.println("Wrong Format");
             //   }
        
        if(s == '2') 
        {
            
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                System.out.print(Math.abs(((y2-y3)*x1-(x2-x3)*y1+x2*y3-x3*y2)/(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)))));
              
        }
        if(s == '3') 
        {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                double a1=0;
                double a2=0;
                a1=(y2-y1)/(x2-x1);
                a2=(y3-y1)/(x3-x1);
                if(a1==a2)
                {
                System.out.println("true");
                }
                else
                {
                 System.out.println("false");
                }
        }
        if(s == '4') {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                x4=Double.parseDouble(k[7]);
                y4=Double.parseDouble(k[8]);
                double a1=0;
                double a2=0;
                double a3=0;
                a1 = (y2-y1)/(x2-x1);
                a2 = (y4-y3)/(x4-x3);
                //a3 = (y3-y2)/(x3-x2);
            if(a1==a2){
                System.out.println("true");
            }
            else{
                System.out.println("false");
            }
            
        }
        if(s == '5') {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                x4=Double.parseDouble(k[7]);
                y4=Double.parseDouble(k[8]);
        }
        System.out.println("outof the triangle");
    }
}

此题考察的是将给出的字符串于所输入的空格来进行分割,期间还穿插着合法数的判断,之后根据逗号识别出相应的有关x,y轴的点位,从而分析出各个点的坐标,再按照题目中的各种要求进行计算。

我的想法是先将所给字符串分割出需要代入运算的数据部分,先进行合法性的判断,如果数据合法,将会对字符串中":"之前的数据进行提取,然后判断运行那一段代码,所代入的数据利用.split()进行分割,再利用Double.parseDouble()将需要进行运算的数据存入以double型规定的x1,x2,x3,x4,y1,y2,y3,y4,其中字符串会根据首个字符来进行不同的分割以及转化存入相对应得x1,x2,x3,x4,y1,y2,y3,y4(x3,x4,y3,y4在执行相应的代码时才会需要),然后进行数学计算,将计算出的结果进行判断,输出对应的结果。

另外还有的方法则是利用第一题所给的线索,在数据合法的情况下用正则表达式去提取所需的数据,再判断数据的位置,判断字符的正负性,最后按要求编写代码,将数据代入计算,从而达到目的。

 

pta4 7-3(源代码如下)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a=in.nextLine();
        String b[] = a.split("\\s");
        String c=in.nextLine();
        String d=in.nextLine();
        String e=in.nextLine();
        String f=in.nextLine();
        BankBusiness bankBusiness = new BankBusiness(b[0],b[1]);
        BankBusiness.welcome();
        bankBusiness.cun(c);
        bankBusiness.cun(d);
        bankBusiness.qu(e);
        bankBusiness.qu(f);
        BankBusiness.welcomeNext();
    }

static class BankBusiness {
    private String name;//客户名称
    private String password;//密码
    double balance;
    public BankBusiness() {
    }

    public BankBusiness(String name, String password) {
        this.name = name;
        this.password = password;
    }
    
    public static String bankName = "中国银行";
    
    public static void welcome() {
        System.out.println(bankName + "欢迎您的到来!");
    }
    public static void welcomeNext() {
        System.out.println("请收好您的证件和物品,欢迎您下次光临!");
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    
    public void cun(String string) {
        String[] a=string.split("\\s");
        if(!password.equals(a[0])) {
            System.out.println("您的密码错误!");
        } else {
            double newBalance=Double.parseDouble(a[1]);
            balance = balance + newBalance;
            System.out.println("您的余额有" + balance + "元。");
        }
    }
    public void qu(String string) {
        String[] a=string.split("\\s");
        if(!password.equals(a[0])) {
            System.out.println("您的密码错误!");
        } else {
            double newBalance=Double.parseDouble(a[1]);
            if(newBalance > balance) {
                System.out.println("您的余额不足!");
            } else if(newBalance == balance){
                balance = 0;
                System.out.println("请取走钞票,您的余额还有" + balance + "元。");
            } else {
                balance = balance - newBalance;
                System.out.println("请取走钞票,您的余额还有" + balance + "元。");
            }

        }
    }

    }
    
}

此题考察的是类,主要目的是让我们更加清楚明白的看清楚使用类的好处,方法与第一题类似,开始出现对象的交互。

期中考试t1(源代码如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        String a=in.next();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        Line line = new Line(point1,point2);
        line.getDistance();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}

此题较为简单

我的想法是先创建一个点类,获得点与点的坐标,之后在创造一个线类,来计算点与点之间的距离,然后通过调用这个类来获取运算过程给出结果,最后将结果给出并且获取结果再按要求print。

 

期中考试t2(源代码如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        Line line = new Line(point1,point2);
        String a=in.next();
        Plane plane = new Plane(a);
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        point1.display();
        point2.display();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        line.getDistance();
        plane.display();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}
class Plane
     {
      String color;
      public Plane(String color)
      {
       this.color = color;
      }
      public String getColor()
   {
    return color;
   }
   public void setColor(String color) 
   {
    this.color = color;
   }
   public void display()
   {
    System.out.println("The Plane's color is:"+this.getColor());
   }
     }

第二题与第一题思路类似,只需要在第一题的基础上加上一个读取字符串的类,将特定的一串字符串读取并且储存,然后再按要求print。

 

期中考试t3(源代码如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        Line line = new Line(point1,point2);
        String a=in.next();
        Plane plane = new Plane(a);
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        point1.display();
        point2.display();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        line.getDistance();
        plane.display();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}
class Plane
     {
      String color;
      public Plane(String color)
      {
       this.color = color;
      }
      public String getColor()
   {
    return color;
   }
   public void setColor(String color) 
   {
    this.color = color;
   }
   public void display()
   {
    System.out.println("The Plane's color is:"+this.getColor());
   }
     }

第三题与第二题类似,在原有的基础上多出来一个容器类,加入容器类,能够是我们更好的对数据进行分析,更好的保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

(3)踩坑心得

心得(1)

String b[] = a.split("\\s");
        String c=in.nextLine();

在编译的时候,如果使用split进行切割时,不能简单地在引号中加入空格,不然会对下一行字符串的读取造成影响,很有可能读取不了数据,在这一个方面困扰了很久,所以我们应该尽量按照标准要求来进行编码,避免出现后续各种不易查出的小问题而影响自己的效率。

心得(2)

静态方法可以直接通过类名调用,任何的实例也都可以调用,因此静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实例方法

心得(3)

继承的出现提高了代码的复用性。  继承的出现让类与类之间产生了关系,提供了多态的前提。

(4)改进建议

1.尽量分文件写代码,这样能够方便我们在日后的学习中检查和修改代码。

2.在进行情况分析时,大多数情况下我都是用枚举法来完成,这一方法的问题在于程序代码过于复杂且重复代码过多,修改起来非常麻烦,在今后的编码过程中,对于简单地判断我会分析枚举法和灵活的方法,判断两种方法的难易程度和简洁程度来运用,简化代码,挺高代码的利用效果,减少不必要的过程。

3.在之后的的代码尽量使用类来进行编写,并且注重细节,避免以后在关键是出现不易出现的问题而影响实验。

4.在日后写代码的过程中不断优化代码,灵活运用所学的知识如(继承,多态,重载)来减少代码的复杂度,提高代码的运算效率

(5)总结

 1.通过两次阶段性的题目集,我从中学了很多,比如初步认识了java语言的语法,对日后的学习打下基础。

 2.对于这两次阶段性的题目集来说,题目本身并不是特别难,做不出的原因主要是自己在课后没有花足够的时间在java的学习上,总的来说就是基础不牢,有句话说的好,基础不牢,地动山摇,所以我会在之后的学习中加强对基础知识的复习与新知识的预习,提高学习效率,多翻阅csdn 博客园上大佬的文献,丰富自己的阅历。

 3.这两次题目集总的来说代码较为简洁,但我写出来的代码过于复杂,嵌套的内容过多,且多数采用枚举法来完成对一个数据的判断,这是一个不好的现象,这样写出来的代码只是对题目来说完成了,但是对日后真正的实际应用没有任何作用,所以在今后我将会将代码写简洁,变灵活,增强代码的可实用性。

4.对于java的学习来说我是有所懈怠的,每次题目集发布后我都是过几天后打开进行作业,这是一种拖拉的现象,在日后我将会尽快完成作业,达到一个自主学习的状态,提高学习的积极性,为下一主要内容类做出努力。

5.加强日后对于java的复习与巩固,提高课外的练习水平。

 

标签:String,Point,System,BLOG,point1,point2,public
来源: https://www.cnblogs.com/tzygood/p/16248824.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有