前三次PTA作业总结
一、前言
1.第一次作业
第一次作业所运用的知识点比较基础,包括整数,浮点数,字符串的输入输出;对整数,浮点数数据的计算与处理;for循环语句;if,switch选择语句和字符串中特定字符的判断。考察的知识点全面、丰富多样,题量适中,难度适中。
2.第二次作业
第二次作业主要考察对字符串的分析与提取,其中str.charAt(i)函数作为提取字符串中指定字符的工具,贯穿三个题目,起了重要的作用。
三道题中第二道较其他两道难度更大,难点主要在于对题目的分析与字符串的相关知识的运用。
3.第三次作业
第三次作业的三道题是关于点线面的程序设计,所需的知识有类的创建和正则表达式的使用,难度随着点线面的要求的提高而提升。
前两次的作业还能用面向程序编程的思维解题,而这次作业让我真正认识到了为什么java叫面向对象编程,为什么要用类来设计程序,深刻反思平时的思维习惯与重新思考java的类的使用。
二、设计与分析
1.第一次作业
判断三角形类型:(1)数据非法
(2)数据合法:①三条边不能构成三角形②一般三角形③等边三角形④直角三角形⑤等腰三角形⑥等腰直角三角形
思路:
1.输入三个数(三角形的三条边)
初判断——数据是否为正数且符合题干;
2.判断{
再判断——数据是否能构成三角形;
若能构成三角形, 接着判断是否为
<1>等腰三角形{ 等腰三角形
等腰直角三角形
<2>等边三角形
<3>直角三角形
若三者皆不是,则为一般三角形;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner num=new Scanner(System.in);
double a = num.nextDouble();
double b = num.nextDouble();
double c = num.nextDouble();
while(true)
if((a<1||a>200)||(b<1||b>200)||(c<1||c>200))
{
System.out.println("Wrong Format");
break;
}
else
{
if(a+b>c&&a+c>b&&b+c>a)
{
if(a==b&&a!=c||a==c&&b!=c||c==b&&c!=a)
{
if(a*a+b*b<c*c+0.000001||b*b+c*c<a*a+0.000001||a*a+c*c <b*b+0.000001)
{
System.out.println("Isosceles right-angled triangle");
break;
}
System.out.println("Isosceles triangle");
break;
}
else if(a==b &&b ==c)
{
System.out.println("Equilateral triangle");
break;
}
else if(a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c ==b*b)
{
System.out.println("Right-angled triangle");
break;
}
System.out.println("General triangle");
break;
}
System.out.println("Not a triangle");
break;
}
}
}
度量值:
2.第二次作业
思路:
判断{ 输入数据有没有起始位(全是1){否——null data
或数据是否足11位 是——{ 奇偶校验错误——parity check error
结束符不为1——validate error
结束符和奇偶校验均不合格——validate error
结束符和奇偶校验均合格——输出由0、1组成的二进制数据流
分析:
这次作业完全是面向过程的。只有一个主类,一Main到底。本题难度在于对数据的处理与分析,将题目逻辑厘清后,代码部分实现采用在for循环以10为一循环周期,进行多次if语句的判断,判断结束符是否为1,和是否满足奇偶校验。
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner n = new Scanner(System.in);
String str = n.next();
int length =str.length();
int flag = 1,j = 0,r=0;
for(j = 0;j<length;j++)
{
if(str.charAt(j)=='1')
{
r++;
}
}
if(r==length||length<11)
{
flag =0;
}
if(flag ==0)
{
System.out.print("null data");
}
else
{
int t = 0,i = 0,num = 1,s = 0;
int e=0,f=0;
for(t = 0;t<length-10;t++)
{
if(str.charAt(t)=='0')
{
System.out.print(num+":");
num++;
if(str.charAt(t+10)=='0')
{
f=0;
}
else
{
f=1;
s = 0;
for(i=t+1;i<t+9;i++)
{
if(str.charAt(i)=='1')
{
s++;
}
}
if(s%2==0)
{
if(str.charAt(t+9)=='1')
{
e=1;
}
else
{
e=0;
}
}
else
{
if(str.charAt(t+9)=='0')
{
e=1;
}
else
{
e=0;
}
}
}
if(f==1)
{
if(e==1)
{
for(i=t+1;i<t+9;i++)
{
System.out.print(str.charAt(i));
}
System.out.print("\n");
}
else
{
System.out.println("parity check error");
}
}
else
{
System.out.println("validate error");
}
t = t + 10;
}
}
}
}
}
度量值:
3.第三次作业
计算两点之间的距离:
思路:
1.输入坐标
合法,但坐标超过两个
2.判断{ 合法,坐标不超过两个 { 计算距离
非法,坐标超过两个
非法,坐标不超过两个
分析:运用的错误的面向过程编程的思路,虽然程序能运行,但不简洁,在计算点的个数时代码冗长,且间接影响到后续的线与面的答题。
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=new String();
str=sc.nextLine();
int i,length,j=0,k=0,t1=0,t2=0,t3=0;
length=str.length();
for(i=0;i<length;i++)
{
char c = str.charAt( i );
if(c == ' ')
{
t2=i;
k=t2;
}
}
for(i=0;i<k;i++)
{
char c = str.charAt( i );
if(c == ',')
{
t1=i;
}
}
for(;k<length;k++)
{
char c = str.charAt( k );
if(c == ',')
{
t3=k;
}
}
for(i=0;i<length;i++)
{
char c = str.charAt( i );
if(c == ',')
{
j++;
}
}
if(j == 2)
{
if(str.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
String str2=str.substring(0,t1);
String str3=str.substring(t1+1,t2);
String str4=str.substring(t2+1,t3);
String str5=str.substring(t3+1,length);
Double a = Double.parseDouble(str2);
Double b = Double.parseDouble(str3);
Double c = Double.parseDouble(str4);
Double d = Double.parseDouble(str5);
pointdome p1 = new pointdome();
p1.setx(a);
p1.sety(b);
p1.setx(c);
p1.sety(d);
dis(a, c,b, d);
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(str.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
public static void dis(double a,double c,double b,double d)
{
double sum = Math.sqrt((a-c)*(a-c)+(b-d)*(b-d));
System.out.println(sum);
}
}
class pointdome {
private double x;
private double y;
public pointdome() {
}
public pointdome(double x,double y ) {
this.x = x;
this.y = y;
}
public void setx( double x){
this.x = x;
}
public double getx() {
return x;
}
public void sety(double y) {
this.y = y;
}
public double gety() {
return y;
}
}
度量值:
线的计算
由于沿用第一题的错误的面向过程编程的思路,导致大于两个坐标的点与线,线与线的计算功能无法实现,故程序存在功能缺失。
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=new String();
str=sc.nextLine();
int i,length,j=0,k=0,t1=0,t2=0,t3=0,t4=0;
length=str.length();
for(i=0;i<length;i++)
{
char c = str.charAt( i );
if(c == ' ')
{
t2=i;
k=t2;
}
}
for(i=0;i<k;i++)
{
char c = str.charAt( i );
if(c == ',')
{
t1=i;
}
}
for(;k<length;k++)
{
char c = str.charAt( k );
if(c == ',')
{
t3=k;
}
}
for(i=0;i<length;i++)
{
char c = str.charAt( i );
if(c == ',')
{
j++;
}
if(c ==':')
{
t4=i;
}
}
String w=str.substring(0,t4);
String y=str.substring(t4+1,length);
int r= Integer.parseInt(w);
if(r == 1)
{
if(j == 2)
{
if(y.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
String str2=y.substring(0,t1-2);
String str3=y.substring(t1-1,t2-2);
String str4=y.substring(t2-1,t3-2);
String str5=y.substring(t3-1,length-2);
Double a = Double.parseDouble(str2);
Double b = Double.parseDouble(str3);
Double c = Double.parseDouble(str4);
Double d = Double.parseDouble(str5);
pointdome p1 = new pointdome();
p1.setx(a);
p1.sety(b);
p1.setx(c);
p1.sety(d);
judge.dis(a, c,b, d);
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else if (y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
if(r == 2)
{
if(j == 3)
{
if(y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(y.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)")||y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
if(r == 3)
{
if(j == 3)
{
if(y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(y.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)")||y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
if(r == 4)
{
if(j == 4)
{
if(y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(y.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else if (y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
if(r == 5)
{
if(j == 4)
{
if(y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
{
}
else
{
System.out.printf("Wrong Format");
}
}
else
{
if(y.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else if (y.matches("[+-]?([0-9]+(\\\\.[0-9]+)?),[+-]?([0-9]+(\\\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true)
System.out.printf("wrong number of points");
else
System.out.printf("Wrong Format");
}
}
}
}
class judge
{
public static void dis(double a,double c,double b,double d)
{
//double sum = Math.sqrt((a-c)*(a-c)+(b-d)*(b-d));
if(a==c)
System.out.printf("Slope does not exist");
else
{
double z;
z=(b-d)/(a-c);
System.out.println(z);
}
}
}
class pointdome
{
private double x;
private double y;
public pointdome() {
}
public pointdome(double x,double y ) {
this.x = x;
this.y = y;
}
public void setx( double x){
this.x = x;
}
public double getx() {
return x;
}
public void sety(double y) {
this.y = y;
}
public double gety() {
return y;
}
}
三、采坑心得
1. 在测试等腰直角三角形时,测试点一直不通过,在这个测试点上停滞了很久,后来想到等腰三角形三边之比为1:1:√2,总有一边为无限不循环小数,去CSDN上查,发现是double类型造成的精度丢失。以前c语言也出现过类似精度丢失问题,看来还是个人的知识没掌握牢靠,属于是一个坑摔两次了。希望通过这次写博客,能吃一堑长一智,下次碰到类似错误时能不要再错了。
2.java第一次做判断格式的类型,没用正则表达式前有点没头绪,后来听同学说用if语句去判断也能判断出来,但我还是尝试了用正则表达式做,感觉比用if语句简洁不少,虽然感觉判断浮点数的这串代码 if(str.matches("[+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?) [+-]?([0-9]+(\\.[0-9]+)?),[+-]?([0-9]+(\\.[0-9]+)?)") == true) 可读性有点差,因为判断浮点数的正则表达式"[+-]?([0-9]+(\\.[0-9]+)?)有点难看懂,但这肯定是我个人的问题,难一眼看懂得地方加句注释就能解决,因为代码简洁好用,运行流畅才是程序所需要的。
3. 做点线面的三道题时,思维固化,一心想着C语言类似的流程,导致真的写成了面向程序编程,若是用类构建坐标,第二题也就不会因为坐标个数而卡住,从而影响后续程序的设计与实现,让我感受到java的面向对象编程的重要性。
四、改进建议
1.第一次作业7-6还存在小问题,忽略了年级对数据的影响导致非法输入测试不通过。
2. 第二次作业7-3存在瑕疵,程序经历了两次遍历,希望以后能减少代码中的重复行为。
3.第三次作业做的不够好,一是要学会建立更规范的类,如斜率double slope();距离 double dis(Point p);三点一线 boolean isonline (Point p);平行 bollean isparallel(Line l);二是要多思考类中要封装的功能,如point类中包含计算两点距离的功能等等诸如这类问题。
五、总结
有痛失分数的懊悔,也有巨大的收获。这三次作业让我的Java中整数,浮点数,字符串的输入输出得到了练习,字符串的分析与提取,字符串特定字符的判断有了一个系统的练习与巩固。 对Java的程序设计有了新的认识,与c语言的面向过程编程不同的是,Java是面向对象编程,逻辑思维的惯性导致思考时总想着原来的思路,导致想法被局限。所以,通过这几次pta作业,所反映的问题首先就是需要熟悉正则表达式的各种情况下的运用,然后最最重要的就是面向对象的思维,要打破常规思维的束缚,培养面向对象的思维,不然对Java的学习会很困难。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现