【发布时间】:2016-04-30 11:14:51
【问题描述】:
我有一个错误,第 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;
}
}
【问题讨论】:
-
firstenemyfirsthealth来自哪里? -
对不起,伙计们,我搞砸了我要找的东西。它实际上是第 61 行,变量“firstbattlerealactualhealth”,它应该等于变量“HP”。它首先出现在第 44 行,它等于被怪物带走的生命值。
-
所以
HP = firstbattlerealactualhealth;出现错误?如果是这样的话,也许你应该在你的问题中写出来 -
对不起,我不知道。