快乐,饥饿,能源状态

时间:2014-03-31 19:33:38

标签: java loops

我正在使用Eclipse设计一个带有Jpanel接口的简单tamagotchi。我试图决定在使用循环编写快乐,饥饿,能量状态的代码时使用的最佳方法,这些是我的两个类,我使用抽象Animal类,而MyPet扩展了Animal类:

动物类:

package GroupAnimal;

import java.util.*;


public abstract class Animal
{
    protected int hungry;
    protected int happy;
    protected int energy;
    protected int toilet;
    protected int love;
    protected int health;
    protected int limbs;
    protected String name;
    protected String dob;
    protected double weight;
    protected double height;


    public int getHungry()
    {
        return hungry;
    }

    public int getHappy()
    {
        return happy;
    }

    public int getEnergy()
    {
        return energy;
    }

    public int gettoilet()
    {
        return toilet;
    }

    public int getLove()
    {
        return love;
    }

    public int getLimbs()
    {
        return limbs;
    }

    public int getHealth()
    {
        return health;
    }

    public String getName()
    {
        return name;
    }

    public String getDob()
    {
        return dob;
    }

    public double getWeight()
    {
        return weight;
    }

    public double getHeight()
    {
        return height;
    }

    protected void setName(String newName)
    {
        name = newName;
    }

    protected void setHeight(double newHeight)
    {
        height = newHeight;
    }

    protected void setWeight(double newWeight)
    {
        weight = newWeight;
    }

    protected void setLimbs (int newLimbs)
    {
        limbs = newLimbs;
    }

    protected void setDOB (String newDOB)
    {
        dob = newDOB;
    }

    protected abstract void die();

    protected abstract void feed(int amount);

    protected abstract void sleep (int amount);

    protected abstract void play (int amount);

    protected abstract void annoy (int amount);

    protected abstract void cuddles (int amount);

    protected abstract void sick (int amount);
}

MyPet:

package IndividualPet; 

import GroupAnimal.Animal;


public class MyPet extends Animal
{

int Dec = 10;
int Inc = 10;


public MyPet(String aName, String aDOB,  int aRelease, int aLimbs, 
        double aWeight, double aHeight, int aEnergy, int aHungry) {
energy = aEnergy;
hungry = aHungry;
name = aName;
dob = aDOB;
toilet = aRelease;
limbs= aLimbs;
weight= aWeight;
height= aHeight;

}


    // TODO Auto-generated constructor stub






enum Skill { ROOKIE, ADVANCED, EXPERT}
enum Concentration { LOW, MEDIUM, STRONG; }
enum Hygiene { POOR, GOOD, EXCELLENT; }



public void decHungry() {


    hungry -= Dec;

}

public void incEnergy() {

    energy += Inc;

}


@Override
protected void die() {
    Runtime.getRuntime().halt(0);{
    System.out.println("Your pet has died");
    }
}
@Override
protected void feed() {

     System.out.println("\nEating..."); 



       decHungry();
       incEnergy();

        if (hungry < 0 && energy > 100) {
            hungry = 0;
            energy = 100;
            System.out.println("I have ate enough!");
        }

        System.out.println("I've just ate dumplings, my current energy state is " + energy 
                +" and hungry state is " + hungry); 
        }



@Override
protected void sleep(int amount) {
    // TODO Auto-generated method stub

}
@Override
protected void play(int amount) {
    // TODO Auto-generated method stub

}
@Override
protected void annoy(int amount) {
    // TODO Auto-generated method stub

}
@Override
protected void cuddles(int amount) {
    // TODO Auto-generated method stub

}
@Override
protected void sick(int amount) {
    // TODO Auto-generated method stub

}

}

因此,这种方法可以让我减少饥饿状态并增加能量状态,尽管当它越界时会保持打印状态 **吃... 我只吃饺子,我目前的能量状态是100,饥饿状态是0

...饮食 我吃够了! 我只吃饺子,我目前的能量状态是100,饥饿状态是0

...饮食 我吃够了! 我只吃饺子,我目前的能量状态是100,饥饿状态是0 **

如何删除除#34之外的所有打印声明;我吃得足够多了!&#34;什么时候超出范围?

@Override
protected void feed() {

     System.out.println("\nEating..."); 



       decHungry();
       incEnergy();

        if (hungry < 0 && energy > 100) {
            hungry = 0;
            energy = 100;
            System.out.println("I have ate enough!");
        }

        System.out.println("I've just ate dumplings, my current energy state is " + energy 
                +" and hungry state is " + hungry); 
        }

任何人都可以举出任何具体的例子来说明如何做到这一点?

1 个答案:

答案 0 :(得分:0)

虽然你的问题太宽泛而无法给出实际答案,但这里有一些一般性建议:

喂养您的宠物是您计划的业务逻辑的一部分。这是你的程序所做的

您希望向用户显示某种动画进度是程序的用户界面的一部分。这是您的程序呈现对用户所做的事情的方式。

如果你有意识地将这两件事分开,你会发现事情变得更加顺利。您需要在用户界面代码中向提供更高级别的用户,而不是在逻辑所在的MyPet内。

例如,首先定义您的实际意图。如果你的意图是“给动物X食物每秒一次,Y次”,那么像(伪代码):

in ui:
   display progress dialog or progress bar
   create a 1 second timer and start it

in timer:
   animal.feed(X);
   if this is the Yth time it happened:
      stop timer
      close dialog / hide progress bar
   end if

在那里使用timer而不是直接循环的目的是允许Swing在延迟期间继续处理事件(因此您的UI不会冻结),并且还可以更直接地确保{ {3}}

如果您的目的是给动物N食物,那么只需制作一个花哨的动画,以便用户获益:

in ui:
   animal.feed(N)
   show and animate a progress bar, even though feeding logic has already run

两者都导致相同的最终结果,两者都有效,但重点是,保持UI和逻辑分离,并使您的代码反映您的意图。请注意,上述伪代码示例都没有涉及在feed()中放置任何与UI相关的内容,并且任何循环(如果已完成)纯粹是UI的一个功能,并且在feed()中也没有。

我希望这很有帮助。我会给你更具体的例子,但是有太多的可能性。当你遇到更具体的问题时,你必须首先尝试一些事情,然后回到这里。