我正在用Eclipse設計一個帶有Jpanel接口的簡單電子郵件地址簿。我試圖決定在使用循環編寫快樂,飢餓,能量狀態代碼時使用什麼是最好的方法,這是我的兩個類,我使用抽象動物類,以及擴展動物類的MyPet:快樂,飢餓,能量狀態
動物類:
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 **
如何刪除除「我已經吃夠了!」之外的所有打印語句。當它超出界限?
@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);
}
任何人都可以扔在如何做到這一點的任何具體的例子嗎?
也許只有一個循環用於遊戲滴答,然後通過單一方法操作邏輯來確定每次滴答做什麼?或甚至使用'ScheduledFuture'來重複任務? – Rogue
所以你要求我們爲你寫代碼? –
五顏六色的標題:-D – Coffee