题目集1-3的总结

发布时间 2023-10-07 23:46:05作者: thefloweryou

一.前言

第一次题目比较简单,题目量偏多,主要是基本的程序设计和基本语法操作。这次题目包含的知识点有:if()选择结构、Java的输入对象的建立、for()循环的使用、Java的控制台输出、字符串的相关函数。

第二次的题目量和第一次一样,难度适中,此次题目中包含的知识点有:定义类和创建对象、字符的分割与相关运算、数组对象的建立申请与相关使用、Boolean变量类型的使用、swich――case的使用、通过引用变量访问对象。

第三次题目较难,题目量偏少,此次题目中包含的知识点有:类的定义、String类相关的使用。

二.设计与分析

 1.题目集2的7-1

class Student{
    private String studentNum;
    private String studentName;
    private int gradeChinese;
    private int gradeMath;
    private int gradePhysics;

    public Student(String studentNum,String studentName,int gradeChinese,int gradeMath,int gradePhysics){
        this.studentNum = studentNum;
        this.studentName = studentName;
        this.gradeChinese = gradeChinese;
        this.gradeMath = gradeMath;
        this.gradePhysics = gradePhysics;
    }

    public String getStudentNum(){
        return studentNum;
    }

    public String getStudentName(){
        return studentName;
    }

    public int calculateTotalScore(){
        return gradeChinese+gradeMath+gradePhysics;
    }

    public double calculateAverageScore(){
        return (double)calculateTotalScore()/3;
    }
}

代码分析:java的最基本单位是类,通过类去描述现实世界的事物。创建学生类,完成类的成员(成员变量和成员方法)。private用于类中,作用是保护类中的成员变量或者成员方法的数据安全。在student类中,成员变量被private修饰了,所以它不能被外界直接调用,而是在类中被定义了get和set方法之后才可以被外界调用。在类中使用get方法获取变量,使用set方法定义变量被外界调用后的执行方法。当外界给变量赋值时并且调用的是set方法,那么就是执行set方法内的操作。

String[] infoArray = studentInfo.split(" ");
String studentNum = infoArray[0];

DecimalFormat decimalFormat=new DecimalFormat("#.00");
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

分析:用了split()方法使用匹配的分隔符将一个字符串拆分成子字符串。用了DecimalFormat进行四舍五入,DecimalFormat类主要依靠#和0占位符来指定数字的长度。0表示如果位数不足,就用0填充,#表示只要有可能就把数字拉到这个位置。

心得:由本题可知,用private把值进行私有化修饰,封装 : 隐藏内部的实现细节,对外提供公共的访问方式;优点:一是提高程序的安全性,二是提高代码的复用性。

2.题目集2 的7-2

public class Main{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        Student[] stus=new Student[3];

        for(int i=0;i<3;i++){
            String studentInfo=input.nextLine();
            String[] infoArray=studentInfo.split("");
            String studentNum=infoArray[0];
            String studentName=infoArray[1];
            int chinesePingshiGrade = Integer.parseInt(infoArray[2]);
            int chineseQimoGrade = Integer.parseInt(infoArray[3]);
            int mathPingshiGrade=Integer.parseInt(infoArray[4]);
            int mathQimoGrade=Integer.parseInt(infoArray[5]);
            int physicsPingshiGrade=Integer.parseInt(infoArray[6]);
            int physicsQimoGrade=Integer.parseInt(infoArray[7]);

            Grade chineseGrade=new Grade(chinesePingshiGrade,chineseQimoGrade);
            Grade mathGrade=new Grade(mathPingshiGrade,mathQimoGrade);
            Grade physicsGrade=new Grade(physicsPingshiGrade,physicsQimoGrade);
            stus[i]=new Student(studentNum,studentName,chineseGrade,mathGrade,physicsGrade);
        }
            DecimalFormat decimalFormat=new DecimalFormat("#.00");
            decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
            for(Student student:stus){
                int zongfen=student.calculateAllGrade();
                 double average=student.calculateAverage();
                 String foraverage=decimalFormat.format(average);
                System.out.println(student.getStudentNum()+" "+student.getStudentName()+" "+zongfen+" "+foraverage);
            }
        
        
    }

代码分析:创建成绩类,学生类。新建一数组存放学生信息,再利用split()方法分隔学生信息,确保输入格式正确。

心得:Integer是int对应的包装类,integer和String一样,也是不可变类型。本题是使用Integer类的静态方法parseInt()将字符串转换为整数。同时了解到也可以使用Integer类的构造函数将字符串转换为Integer对象,但需要注意的是,在转换之前需要确保字符串中包含的是数字,否则会抛出。

3.题目集2的7-7

import java.util.Scanner;
 
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        
        String[] menu =new String[]{"清炒土豆丝","西红柿炒蛋","油淋生菜","麻婆豆腐"};
        int price=0;
        for(;;){
            int count=0;
            String dish=sc.next();
            int p;
            if(dish.equals("end"))
                break;
            p=sc.nextInt();
            if(dish.equals(menu[1])){
                count=1;
                if(p==1)
                    price=price+15;
                if(p==2)
                    price=price+23;
                if(p==3)
                    price=price+30;
            }
            else if(dish.equals(menu[0])){
                count=1;
                if(p==1)
                    price=price+12;
                if(p==2)
                    price=price+18;
                if(p==3)
                    price=price+24;
            }
            else if(dish.equals(menu[3])){
                count=1;
                if(p==1)
                    price=price+12;
                if(p==2)
                    price=price+18;
                if(p==3)
                    price=price+24;
            }
            else if(dish.equals(menu[2])){
                count=1;
                if(p==1)
                    price=price+9;
                if(p==2)
                    price=price+14;
                if(p==3)
                    price=price+18;
            }
            if(count==0)
                System.out.println(dish+" does"+" "+"not"+" "+"exist");
        }
        System.out.println(price);
    }
}

代码分析:声明一个字符串数组menu[]存储菜单中的菜名,进入一个无限循环,直到输入“end”结束循环;在循环中首先初始化count为0,读取用户输入的菜名并存储在变量dish中,通过判断用户输入的菜名和对应的索引,来计算并更新价格。

心得:对于类的构建有多种方法,构造好类,并建立相应的对象可以大大减少代码的行数于是代码的结构更加的简单。而我对类的接触并不多,开始写类也是按照书上的例子,一旦创建太多类,对其调用也不熟悉。

4.题目集3的7-2

由于本题65分,审题之后对我来说难度较高。

 

import java.text.Collator;
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String s_record = s.nextLine();
        ParseInput handle=new ParseInput();
        while (!s_record.equals("end")) {
            handle.parseInput(s_record);//解析用户输入的每一行数据
            s_record = s.nextLine();
        }
        handle.showStudents();
        handle.showCourses();
        handle.showClasses();
    }
}

 

 

三.踩坑心得

1、提交答案时,Java的代码的public修饰的类名必须时以Main命名的,且不能带入Java代码文件储存报包的开头,否则会报错。

2、nextLine()和next()有所区别,前者返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。后者会自动地消除有效字符之前的空格,只返回输入的字符,不会得到带空格的字符串。

 

输出时%f输出导致输出小数点位数总是与样例不符合,在java中可直接输出,并且以前C语言可以格式输入,现在java用split方法来分割字符数组从而得到想要的结果。有些题目测试点是格式非法输入判断,过于注意样例的非法输入,忘记独自考虑其他类型的非法输入,导致在做的时候进行了大量的错误操作。还有就是一些小错误,符号匹配以及漏字符的问题。

 

四.主要困难以及改进建议

对于我的部分代码我觉得可以在后期的学习后进一步进行优化改进。很多的地方有多余,在后期很多的结构可以多优化,有些题目的功能其实并没有完全实现,在后期也可以慢慢完善。还有运行超时的问题在其他题目中没有解决,由于循环的问题。再则就是非零返回,数组的越值等问题,具体问题具体分析,在不同的题目中 还是没有解决这些疑点,在后期的学习中,需要请教其他同学。

五.总结

巩固了java的基础知识,对String的使用更加熟悉。通过这三次的题目发现,我写代码依旧不熟练,写的太少,对一些逻辑结构不了解。要给足够的时间去思考如何下手,具体的思路而不要盲目,掌握基本的语法规则,加强面向对象的思维。对待难题时,不要有畏难心理要敢于面对,能写一点是一点。

对于这三次题目集我觉得是一个循序渐进的过程,我觉得每周一个大作业可以让我们主动去学习一些代码相关的知识,但到最后题目的难度的确增长的太快了,对我来说太难了, 因此我希望以后的较难题目可以由老师在课堂上进行相应的讲解这样我相信我们可以学到更多的知识。