电子宠物加强版

Posted 敲不完的代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电子宠物加强版相关的知识,希望对你有一定的参考价值。

1、Pet 父类

package com.hanqi.maya.model;

import java.util.Date;

/**
 * 宠物类
 * 健康值不能小于5
 * 饥饿值5~95
 * 开心值:如果小于30,每次有活动,健康值-2
 * 
 * @author asus
 *
 */
public abstract class Pet {
    private String name;
    private String sex;
    private int age;
    private Date createtime;
    private int happy;
    private int healthy;
    private int hungry;
    private boolean isAlive;
    
    public Pet(String name, String sex) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = 1;
        this.createtime = new Date();  //注意Date
        this.happy = 90;
        this.healthy = 100;
        this.hungry = 80;
        this.isAlive = true;
    }
    
    public abstract void eat();//含有抽象方法的类必须声明为抽象类,所以类名那里也要加上abstract
    public abstract void play();
    public abstract void hospital();
    
    public void check(){
        if(healthy < 5 || hungry < 5 || hungry > 95){
            isAlive = false;        
        }
        if(happy < 30){
            healthy -= 2;
        }
        if(healthy > 100){
            healthy = 100;
        }
        if(happy > 100){
            happy = 100;
        }
        this.setAge((int)((System.currentTimeMillis() - createtime.getTime()) /1000 /5) + 1);
    }
    
    public void showInfo(){
        check();
        System.out.println("宠物信息:");
        System.out.println("\t名字: "+name+",性别: "+sex+",年龄: "+age);//  \t转义制表符,相当于Tab键
        System.out.println("\t开心值: "+ happy);
        System.out.println("\t健康值: "+ healthy);
        System.out.println("\t饥饿值: "+ hungry);
    }
    
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getCreatetime() {
        return createtime;
    }
    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }
    public int getHappy() {
        return happy;
    }
    public void setHappy(int happy) {
        this.happy = happy;
    }
    public int getHealthy() {
        return healthy;
    }
    public void setHealthy(int healthy) {
        this.healthy = healthy;
    }
    public int getHungry() {
        return hungry;
    }
    public void setHungry(int hungry) {
        this.hungry = hungry;
    }


    public boolean isAlive() {
        return isAlive;
    }


    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }
}

2、Rabbit 子类

package com.hanqi.maya.model;

public class Rabbit extends Pet {

    public Rabbit(String name, String sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "拿到一根萝卜在吃,饥饿值-20");
            this.setHungry(this.getHungry() - 20);
            check();
        }else{
            System.err.println("你的宠物挂了");
        }    
    }

    public void play() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "玩了一个魔方,饥饿值+5,开心值+10");
            this.setHungry(this.getHungry() + 5);
            this.setHappy(this.getHappy() + 10);
            check();
        }else{
            System.err.println("你的宠物挂了");
        }    
        
    }

    public void hospital() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "在医院里哇哇叫,饥饿值+20,健康值+50");
            this.setHungry(this.getHungry() + 20);
            this.setHealthy(this.getHealthy() + 50);
            check();
        }else{
            System.err.println("你的宠物挂了");
        }    
        
    }
    
}

3、Main方法

package com.hanqi.maya.text;

import java.util.Scanner;

import com.hanqi.maya.model.Pet;
import com.hanqi.maya.model.Rabbit;

public class Main {
    public static void main(String[] args) {
        //Pet p = new Rabbit("旺财", "男");
        //p.showInfo();
        Pet p = null;
        
        Scanner scanner = new Scanner (System.in);
        
        boolean selectFlag = true;
        boolean circleFlag = true;
        /*
        System.out.println("请输入宠物类型");
        System.out.println("1--兔子");
        */
        while(circleFlag){
            if(selectFlag){
                String s = scanner.nextLine();
                System.out.println("输入宠物的姓名和性别,用逗号分开");
                String info = scanner.nextLine();
                String[] infos = info.split(",");
                if("1".equals(s)){
                    p = new Rabbit(infos[0],infos[1]);
                    selectFlag = false;
                }
            }
            printControl();
            String ss = scanner.nextLine();
            if("1".equals(ss)){
                p.showInfo();
            } else if("2".equals(ss)){
                p.eat();
            } else if("3".equals(ss)){
                
            } else if("4".equals(ss)){
                
            } else if("5".equals(ss)){
                System.out.println("再见");
                circleFlag = false;
            } else{
                System.out.println("无此操作");
            }
        }
        scanner.close();
    }
    private static void printControl(){
        System.out.println("输入想进行的操作");
        System.out.println("1---显示信息");
        System.out.println("2---吃饭");
        System.out.println("3---玩游戏");
        System.out.println("4---去医院");
        System.out.println("5---退出");
    }
}

 

以上是关于电子宠物加强版的主要内容,如果未能解决你的问题,请参考以下文章

有道二加强版如何安装第三方软件

二柱子课后题加强版的加强版

P3796 模板AC自动机(加强版)

这些角度电子邮件指令代码片段如何连接

bzoj3767A+B Problem加强版

OJ1147括号匹配加强版(栈)