我刚开始使用Java,并且为了学习它,我试图在BlueJ中编写Tamagotchi。这是代码的一部分:
public class Tamagotchi
{
private int hunger;
private int mood;
private int fatigue;
private int hBorder;
private int mBorder;
private int fBorder;
private String generalCondition;
public Tamagotchi(int hungerBorder, int moodBorder, int fatigueBorder)
{
// Instanzvariable initialisieren
hunger = 0;
mood = 0;
fatigue = 0;
hBorder = hungerBorder;
mBorder = moodBorder;
fBorder = fatigueBorder;
generalCondition = "indifferent";
}
public void setGeneralCondition(){
if (fatigue > fBorder){
generalCondition = "tired";
}
if ((fatigue < fBorder) & (hunger > hBorder)){
generalCondition = "hungry";
}
if ((mood > mBorder) & (hunger < hBorder) &
(fatigue < fBorder)){
generalCondition = "happy";
}
else {
generalCondition = "indifferent";
}
}
public void play()
{
if (hunger > hBorder){hunger += 2; mood += 2; fatigue += 3;
}
else {}
setGeneralCondition();
}
public void eat(){
if (fatigue > fBorder){hunger -= 2; fatigue += 2;
}
else{}
setGeneralCondition();
}
public void sleep(){
if (hunger > hBorder){hunger += 1; mood -= 1; fatigue = 0;
}
else{hunger += 1; mood += 1; fatigue = 0;
}
setGeneralCondition();
}
public void pet(){
hunger += 1;
mood += 2;
setGeneralCondition();
}
public String getGentralCondition(){
return generalCondition;
}
public void makeHappy(){
eat();
sleep();
}
}
我的问题是,当我使用任何方法时,
generalCondition
都不会更改。我认为方法setGeneralCondition
出错了,但是我不知道问题出在哪里。有人可以帮助:/?提前THX。
编辑:
我现在发布了孔代码。就像我在评论中说的:每当我使用eat(),play()之类的方法时,我都希望
generalCondition
进行更改。方法
setGeneralCondition
应该检查饥饿情绪或疲劳值之一是否超过某个边界并更改条件(变为疲劳,饥饿或快乐)。但是,只要我使用其他方法之一,generalCondition
都不会改变。 最佳答案
编辑:我对您的代码进行了进一步的编辑,并提出了一个运行您的代码并更改generalCondition的小主程序!签出并运行以供自己查看。
我的直觉告诉我,这个类Tamagotchi是一个类文件或对象Tamagotchi,将由驻留在另一个文件中某个地方的main方法使用。如果真是这样,我相信这是一个简单的修复,您要做的就是在很多地方添加this.
。我已经开始为您做下面的工作。试试这个,让我知道这是否有效!
public class Tamagotchi{
private int hunger;
private int mood;
private int fatigue;
private int hungerBorder;
private int moodBorder;
private int fatigueBorder;
private String generalCondition;
public Tamagotchi(int hungerBorder, int moodBorder, int fatigueBorder)
{
// Instanzvariable initialisieren
this.hunger = 30;
this.mood = 30;
this.fatigue = 30;
this.hungerBorder = hungerBorder;
this.moodBorder = moodBorder;
this.fatigueBorder = fatigueBorder;
this.generalCondition = "indifferent";
}
public void setGeneralCondition(){
if (fatigue > fatigueBorder){
this.generalCondition = "tired";
return;
}
if ((fatigue < fatigueBorder) && (hunger > hungerBorder)){
this.generalCondition = "hungry";
return;
}
if ((mood > moodBorder) && (hunger < hungerBorder) && (fatigue < fatigueBorder)){
this.generalCondition = "happy";
return;
} else {
this.generalCondition = "indifferent";
}
}
public String getGeneralCondition(){
return generalCondition;
}
public void play()
{
if (!generalCondition.equalsIgnoreCase("hungry")){
hunger += 2; mood += 2; fatigue += 3;
} else {
}
setGeneralCondition();
}
public void eat(){
if (fatigue > fatigueBorder){
hunger -= 2; fatigue += 2;
} else{
}
setGeneralCondition();
}
public void sleep(){
if (hunger > hungerBorder){
hunger += 1; mood -= 1; fatigue = 0;
} else{
hunger += 1; mood += 1; fatigue = 0;
}
setGeneralCondition();
}
public void pet(){
hunger += 1;
mood += 2;
setGeneralCondition();
}
public void makeHappy(){
eat();
sleep();
}
}
这是我编写的用于测试上面代码的一些主要方法。
public class Test {
public static void main(String[] args) {
Tamagotchi test = new Tamagotchi(10, 10, 10);
test.play();
test.makeHappy();
System.out.println(test.getGeneralCondition());
}
}
输出:
hungry
希望这可以帮助!
附言这是基本编码样式的链接。使用这些样式练习编码,因为它可以使您自己的代码更容易为他人阅读,看起来更简洁,并且更容易查看和修复错误! Coding Style Guide
关于java - Java:参数的值不变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57400192/