变量超出范围
Variable out of Range
我有一个错误,第 61 行指出变量 firstbattlerealactualhealth
超出了使用范围。我尝试了各种方法,但似乎无法正常工作。
如果您能告诉我哪里出了问题,以及我需要解决什么问题,我将不胜感激。
编辑: 更改了哪个变量和行有问题,因为我无法计数或读取。
boolean firstbattle = true;
while (firstbattle) {
int firstbattlehealth = HP;
int firstbattleattack = Attack;
int firstbattledefense = Defense;
int firstbattleMP = MP;
int firstbattleEXP = EXP;
int firstbattlegold = Gold;
int firstattack = (int) Math.ceil(Math.random() * 6);
int firstdefense = (int) Math.ceil(Math.random() * 6);
int firstenemyattack = (int) Math.ceil(Math.random() * 6);
int firstenemydefense = (int) Math.ceil(Math.random() * 6);
int firstenemyhealth = firstenemyfirsthealth;
int firstenemynewhealth = firstenemyfirsthealth;
int firstenemyhitpoints = (int) Math.ceil(Math.random() * 25);
int firsthitpoints = 0;
boolean firstkill = true;
while (firstkill) {
if (firstattack > firstenemydefense) {
firsthitpoints = (int) Math.ceil(Math.random() * firstbattleattack);
System.out.println();
System.out.println("You did " + firsthitpoints + " damge to the Slime.");
}
firstenemynewhealth = firstenemyhealth - firsthitpoints;
if (firstenemynewhealth <= 0) {
firstkill = false;
}
if (firstattack <= firstenemydefense) {
System.out.println();
System.out.println("Attack failed!");
}
int firstbattleactualhealth = firstbattlehealth;
if (firstenemyattack > firstdefense) {
int firstenemyhitpointattack = (int) Math.ceil(Math.random() * firstenemyhitpoints);
int firstbattlerealactualhealth = firstbattleactualhealth - firstenemyhitpointattack;
System.out.println();
System.out.println("The enemy did " + firstenemyhitpointattack + " damage to you. You have " + firstbattlerealactualhealth + " health remaining.");
}
if (firstenemyattack <= firstdefense ) {
System.out.println();
System.out.println("Enemy attack missed!");
}
}
int firstenemyactualhealth = firstenemynewhealth;
if (firstenemyactualhealth <= 0){
System.out.println();
System.out.println("You won the battle!");
HP = firstbattlerealactualhealth;
EXP = firstbattleEXP + 20;
Gold = firstbattlegold + 20;
System.out.println();
System.out.println("You have " + HP + " health.");
System.out.println("You have earned" + EXP + " experience.");
System.out.println("You have earned" + Gold + " gold.");
firstbattle = false;
}
}
可能是因为 firstbattlerealactualhealth
是在 if
子句中定义的。如果程序不执行那个 if
块,firstbattlerealactualhealth
将不会被定义。你必须在你确定它会被执行的地方定义和初始化它。
最重要的是,如果您在一个块内定义一个变量({}
),它应该只在该块内使用,而不是其他任何地方。
提示:
不要命名任何变量 first_sth
。它后面很可能会跟着名称为 second_sth
和 third_sth
的变量,您将无法管理它们。您应该通过传递不同的参数为其他战斗(不仅是史莱姆)重用相同的逻辑,这样您就可以编写 DRY(不要重复自己)代码。变量名也将更易读。
我有一个错误,第 61 行指出变量 firstbattlerealactualhealth
超出了使用范围。我尝试了各种方法,但似乎无法正常工作。
如果您能告诉我哪里出了问题,以及我需要解决什么问题,我将不胜感激。
编辑: 更改了哪个变量和行有问题,因为我无法计数或读取。
boolean firstbattle = true;
while (firstbattle) {
int firstbattlehealth = HP;
int firstbattleattack = Attack;
int firstbattledefense = Defense;
int firstbattleMP = MP;
int firstbattleEXP = EXP;
int firstbattlegold = Gold;
int firstattack = (int) Math.ceil(Math.random() * 6);
int firstdefense = (int) Math.ceil(Math.random() * 6);
int firstenemyattack = (int) Math.ceil(Math.random() * 6);
int firstenemydefense = (int) Math.ceil(Math.random() * 6);
int firstenemyhealth = firstenemyfirsthealth;
int firstenemynewhealth = firstenemyfirsthealth;
int firstenemyhitpoints = (int) Math.ceil(Math.random() * 25);
int firsthitpoints = 0;
boolean firstkill = true;
while (firstkill) {
if (firstattack > firstenemydefense) {
firsthitpoints = (int) Math.ceil(Math.random() * firstbattleattack);
System.out.println();
System.out.println("You did " + firsthitpoints + " damge to the Slime.");
}
firstenemynewhealth = firstenemyhealth - firsthitpoints;
if (firstenemynewhealth <= 0) {
firstkill = false;
}
if (firstattack <= firstenemydefense) {
System.out.println();
System.out.println("Attack failed!");
}
int firstbattleactualhealth = firstbattlehealth;
if (firstenemyattack > firstdefense) {
int firstenemyhitpointattack = (int) Math.ceil(Math.random() * firstenemyhitpoints);
int firstbattlerealactualhealth = firstbattleactualhealth - firstenemyhitpointattack;
System.out.println();
System.out.println("The enemy did " + firstenemyhitpointattack + " damage to you. You have " + firstbattlerealactualhealth + " health remaining.");
}
if (firstenemyattack <= firstdefense ) {
System.out.println();
System.out.println("Enemy attack missed!");
}
}
int firstenemyactualhealth = firstenemynewhealth;
if (firstenemyactualhealth <= 0){
System.out.println();
System.out.println("You won the battle!");
HP = firstbattlerealactualhealth;
EXP = firstbattleEXP + 20;
Gold = firstbattlegold + 20;
System.out.println();
System.out.println("You have " + HP + " health.");
System.out.println("You have earned" + EXP + " experience.");
System.out.println("You have earned" + Gold + " gold.");
firstbattle = false;
}
}
可能是因为 firstbattlerealactualhealth
是在 if
子句中定义的。如果程序不执行那个 if
块,firstbattlerealactualhealth
将不会被定义。你必须在你确定它会被执行的地方定义和初始化它。
最重要的是,如果您在一个块内定义一个变量({}
),它应该只在该块内使用,而不是其他任何地方。
提示:
不要命名任何变量 first_sth
。它后面很可能会跟着名称为 second_sth
和 third_sth
的变量,您将无法管理它们。您应该通过传递不同的参数为其他战斗(不仅是史莱姆)重用相同的逻辑,这样您就可以编写 DRY(不要重复自己)代码。变量名也将更易读。