我正在制作基于文字的游戏。这是一种RPG风格,用户可以获得与数字相关联的选项,他们必须选择一个数字。现在我的问题是在运行程序时。某种方法Decision()只能使用一定的时间。该方法在superClass中,而在子类中调用它。在子类中,它第一次工作,但不一定是第二次。此外,当我将决策方法从超类复制到子类时,它开始工作,但下次调用时,它会停止。这是我尝试过的结果。我已经包含了决策方法以及它被调用的位置。
决策方法:
public int decision(String question, int length, String[] choices){
int[] numbers = new int[length];
int iterator = 1;
for(int i = 0; i < length; i++){
numbers[i] = iterator;
iterator++;
}
boolean done = false;
while(!done){
//print("Test");
print("");
print(question);
String options = "";
for(int i = 0; i < numbers.length; i++){
options = (options + numbers[i] + " - " + choices[i] + " ");
}
print(options);
boolean univSet = true;
int entry = 1;
while(univSet){
if(univInt != 0){
univSet = false;
entry = univInt;
univInt = 0;
//print("testing");
}
}
if(entry == 23){
help();
}else{
for(int i = 0; i < numbers.length; i++){
if(entry == numbers[i]){
done = true;
univInt = 0;
return entry;
}
}
print("Invalid Number, Try again");
print("");
univInt = 0;
}
}
return (Integer) null;
}
第1章类(被称为的地方:
public class Chapter1 extends Story implements Serializable {
Player player;
public Chapter1(Player player){
this.player = player;
}
public void engage() {
// TODO Auto-generated method stub
player.chapter = 1;
save(player.name);
sPrint("Welcome to Chapter 1");
print("You wake up in a lighted room with white walls.\nA fresh breeze is coming through the window yet the air smells rotten.");
print("You jolt up suddenly. You don't remember anything about how you got here. You've even forgotten who you are.");
print("You look down at your white shirt, there is a streak of blood across the top.\nYou are wearing a nametag that says: " + player.name + ".");
print("You're sitting in a chair but there are no restraints. You decide to get up and look around");
cur = decision("What do you do?", 2, new String[]{"Try the door", "Look out the window"});
print(cur + "");
if(cur == 1){
print("You walk over to the door and try and open it, it is unlocked.\nYou walk through and are welcomed by a cold and poorly lit hallway");
}else{
print("You walk to the window and look outside. You see a huge barren field. You can make out a humanoid like structure.\nYou call out yet the figure doesn't move.");
print("You decide to try the door. It's unlocked so you walk through into a cold dimly lit hallway.");
}
print("You see a dull knife on the floor as well as a door on the end of the hallway");
cur = decision("What do you do?", 2, new String[]{"Go to the door", "Take the knife"});
if(cur == 2){
print("You pick up the knife.");
addWeapon("Kitchen Knife", player);
}else{
print("You walk down the hallway to the door when suddenly the door opens and out comes a zombie.\nIt Lunges for your shoulder. You are caught by surprise and it bites into your skin and you are infected");
gameOver();
}
print("You continue to walk down the hall when suddenly a hideous creature lunges out from the door.\nYou jump back and prepare yourself for a battle.");
battle("Zombie", 5, 2, player);
sPrint("I see that you have succeeded in your first encounter with the undead.\nI congratulate you but you have a long way to go. Remember, I am your only way to learning about your past. \nNow, make your way down to the bottom of the tower. I will help you where I see fit along the way.");
print("You look around and see that the lights have brightened up. The zombie has been mutilated by your Kitchen Knife. \nYou don't know where the voice came from but you are scared. Behind the zombie's original hiding spot you see a staircase.\nYou follow it down, onto what seems to be..the 11th floor.");
print("");
print("Please input 'complete' to continue");
pause();
sPrint("Chapter 1 complete");
}
现在在这个类中,调用了engage()来运行本章。决定是在你看到的地方,以及在battle()方法中被调用(战斗方法循环几次,每个循环都调用decision()。
最初,决策和战斗都在超类中,但不在子类中。这导致类中的第一个决策方法被调用,而不是第二个。在第二个中,它在循环处停止检查univInt的值。
当我将决策方法放入子类时,它会传递前两个,但由于同样的原因,它无法超越战斗方法中的第一个。
当我将决策和战斗方法都放入子类时,它与仅仅做出决定具有相同的结果。
最后如果我把战斗放在子类而不是决定它只会再次通过前两个。
在项目中,我有一个名为cur的变量,它包含任何决策返回的整数值。我为每个决定重复使用它。我不知道这与它有什么关系。对我来说,这对方法是否在同一个类中是否真的没有意义,或者如果它们是相同的方法,那么继承是完全重要的。
我准备澄清任何事情,我希望有人能够理解出了什么问题。
编辑:
univInt被设置为决定之外的另一个0以外的数字。这就是为什么它有效的一些。它是一个swing类,并且当按下一个按钮时,超类中的一个方法设置为不同于TextField中的任何内容,因此使用while循环我尝试不断检查以查看univInt已从0更改
答案 0 :(得分:1)
看起来像你的&#34; univInt&#34;是一个类成员,而不是局部变量,并且在进入函数时不会重新初始化它。因此,它不会被改回以允许程序进入你提到的if语句。