0
点赞
收藏
分享

微信扫一扫

java :快乐养宠物操作

女侠展昭 2022-03-30 阅读 81
java

面向对象 设计宠物类
需求:设计宠物类,用户可以自由选择养猫还是养狗,可以给宠物起名字,还可以实现喂食互动的功能,宠物需要有饱食度和快乐度

上代码:

父类:

   String name;
    int full=50;
    int happy=50;
    public void shout(){
        System.out.println("动物会叫");
    }

子类Dog:

public void shout(){
       System.out.println("汪汪汪");
   }
    public void s1(){
        while (true){
            System.out.println("喂食or玩耍");
            Scanner sc1 = new Scanner(System.in);
            String m1 = sc1.next();
            switch (m1){
                case "喂食":
                    System.out.println("请放入食物");
                    String m2 = sc1.next();
                    full+=10;
                    if(full==100){
                        System.out.println("撑死");
                        return;
                    }
                    System.out.println("饱腹感:"+full);
                    break;
                case "玩耍":
                    System.out.println("请输入玩耍方式");
                    String m3=sc1.next();
                    happy+=10;
                    if(happy==100){
                        System.out.println("幸福死了");
                        return;
                    }
                    System.out.println("快乐值:"+happy);
                    break;
                case "惩罚":
                    System.out.println("请输入惩罚方式");
                    String m4= sc1.next();
                    this.shout();
                    happy-=20;
                    if(happy<0){
                        System.out.println("伤心欲绝到死亡");
                        return;
                    }
                    System.out.println("快乐值:"+happy);
                    break;
            }
        }
   }

子类猫:

  public void shout(){
        System.out.println("喵喵喵");
    }
    public void s1(){
        while (true){
            System.out.println("喂食or玩耍");
            Scanner sc1 = new Scanner(System.in);
            String m1 = sc1.next();
            switch (m1){
                case "喂食":
                    System.out.println("请放入食物");
                    String m2 = sc1.next();
                    full+=10;
                    if(full==100){
                        System.out.println("撑死");
                        return;
                    }
                    System.out.println("饱腹感:"+full);
               break;
                case "玩耍":
                    System.out.println("请输入玩耍方式");
                    String m3=sc1.next();
                    happy+=10;
                    if(happy==100){
                        System.out.println("幸福死了");
                        return;
                    }
                    System.out.println("快乐值:"+happy);
                  break;
                case "惩罚":
                    System.out.println("请输入惩罚方式");
                    String m4= sc1.next();
                    this.shout();
                    happy-=20;
                    if(happy<0){
                        System.out.println("伤心欲绝到死亡");
                        return;
                    }
                    System.out.println("快乐值:"+happy);
                 break;
            }
        }
    }

 测试类:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Dog d1 = new Dog();
        Cat c2 = new Cat();
        System.out.println("请选择您的宠物");
        System.out.println("1.猫、2狗");
        int a = sc.nextInt();
        System.out.println("请给您的宠物起名字");
        if(a==1){
           d1.name=sc.next();
           d1.s1();
        }else if(a==2){
            c2.name=sc.next();
            c2.s1();
        }
    }

 这不简简单单?

举报

相关推荐

0 条评论