-2
public class Geniegotchi {
private String name = "Bob";
private int endurance = 4;
private int happiness = 3;
public void setName(String newName){
name = newName;
}
public void setEndurance(int newEndurance){
endurance = newEndurance;
}
public void setHappiness (int newHappiness){
happiness = newHappiness;
}
public String getName(){
return name;
}
public int getEndurance(){
return endurance;
}
public int getHappiness(){
return happiness;
}
public void genieInfo(){
System.out.println("current name: "+this.getName());
System.out.println("current happiness level: "+this.getHappiness());
System.out.println("current endurance level: "+this.getEndurance());
}
public void feed(){
if (this.getEndurance() <= 10){
}
else
System.out.println("No, thanks...");
}
public void play(){
if (this.getHappiness() < 10){
}
else
System.out.println("No, thanks");
}
如果耐力爲 小於10,則無效feed()方法將當前耐力增加1,否則會在屏幕上顯示「No,thanks ...」消息;如果幸福感小於10,則void play()方法將當前的幸福感提高1,並將當前的耐力降低2,否則,這將在屏幕上打印一個 「不,謝謝...」消息;如何將數字添加到實例變量?
對於無效飼料和無效劇的兩部分,我不知道用1到如何增加電流的耐力,並增加1的幸福生活,並通過2 減少電流耐力謝謝
你可以簡單地使用'耐力++;'作爲一個語句來增加你的價值。作爲替代,你也可以調用'setEndurance(1 + getEndurance());' – SomeJavaGuy
非常感謝。 – Chriseagles
我爲您提供使用AtomicInteger。 https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html –