我完成的作业

完成PTA的作业并拿到至少一百分

以上是我完成的内容以及部分题目的代码展示,在写乘法表的时候,由于格式问题参考了CSDN上的一篇文章,[参考文章](【PTA 】Java无输入打印九九乘法表_打印99乘法表输入样例:不需要输入输出样例:在这里给出相应的输出。要求:99乘_全宇宙最帅的帅宝的博客-CSDN博客),发现打印乘法表时没有考虑十位与个位的占位问题,找到了改正方法

完成利扣上删除数组元素的题目

碰壁了好几次,看不懂它错的理由,直到看到左手边的提示,[2,2,0,0]也算正确答案,删除不太会,但我可以把我要删除的元素通过排序排到后方并把他们赋值为零,接着利用冒泡排序的方法,把val值的项排到后方,从而完成了作业

完成西瓜摊的作业

下面是西瓜摊类的方法设计

package com.huayu.work;


public class Booth {
    @Override
    public String toString() {
        return "Booth{" +
                "boothNumber=" + boothNumber +
                ", name='" + name + '\'' +
                ", saleNumber=" + saleNumber +
                ", rectification=" + rectification +
                '}';
    }

    private long boothNumber;

    public long getBoothNumber() {
        return boothNumber;
    }

    public void setBoothNumber(long boothNumber) {
        this.boothNumber = boothNumber;
    }

    public String getName() {
        return name;
    }

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

    public int getSaleNumber() {
        return saleNumber;
    }

    public void setSaleNumber(int saleNumber) {
        this.saleNumber = saleNumber;
    }

    public boolean isRectification() {
        return rectification;
    }

    public void setRectification(boolean rectification) {
        this.rectification = rectification;
    }

    private String name;
    private  int saleNumber;
    private boolean rectification;

    public Booth(long boothNumber,String name,int saleNumber,boolean rectification){
        this.boothNumber=boothNumber;
        this.name=name;
        this.saleNumber=saleNumber;
        this.rectification=rectification;

    }
    public static void purchase(Booth name,int num){
        if (num<=0){
            System.out.println("购买数值出错购买失败");
            return;
        }
       else if (name.isRectification()==true){
            System.out.println("休摊整改购买失败");
            return;
        }
       else if(num> name.saleNumber){
            System.out.println("购买数量过多,购买失败");
            return;
        }
        else{
            name.saleNumber= name.saleNumber-num;
            System.out.println("购买成功");
            return;
        }

    }
    public void restock(int inport){
        if (inport<=200 && inport>0){
            saleNumber=saleNumber+inport;
            System.out.println("进货成功");
        }
        else {
            System.out.println("进货失败");
        }

    }
    public static void closeBooths(Booth[]booths){

        for (Booth booth:booths){

        if (booth.isRectification()==false) {
            booth.setRectification(true);
            System.out.println(booth.toString());

        }

    }}
}

下面是西瓜摊类的输出页面

package com.huayu.work;



public class WorkApplication {
    public static void main(String[] args) {
        Booth booth1 = new Booth(1L, "huayu", 50, false);
        Booth booth2 = new Booth(2L, "lianai", 30, false);
        Booth booth3 = new Booth(3L, "yusiheng", 45, false);
        System.out.println(booth1.toString());
        System.out.println(booth2.toString());
        System.out.println(booth3.toString());
        Booth.purchase(booth1,70);
        Booth.purchase(booth2,2);
        Booth.purchase(booth3,12);
        booth1.restock(500);
        booth2.restock(40);
        booth3.restock(30);
        Booth[] booths={booth1,booth2,booth3};

        Booth.closeBooths(booths);


    }
    }

接着是输出的结果

设计西瓜摊类给我的印象最为深刻,起码对于现在的我而言,是不可多得的好题,前期一直使用面向过程的思想四处碰壁,直到我想,也许能先编写我需要输出,需要做的事情是什么,我才有了思路和方向,它深刻让我意识到面向对象编程的重要性,也引导我进一步对静态方法,变量等,实例方法,实例变量等的更深一步了解,在许多其他地方我也有了进步,比如在一个方法中定义,一个类的类型的变量,这个类的变量就会被覆盖,而不能直接被引用,用this尝试引用时,又学到了this不能在静态方法中使用的原理,这道题目我花的时间最久,感受也最深

posted @ 2023-09-25 23:08  huayulianai  阅读(45)  评论(0)    收藏  举报