Reputation:
I have an error where line 61 states that variable firstbattlerealactualhealth
is out of range to be used. I've tried various things, but I just can't seem to get it to work.
If you could show me what's wrong, and what I need to fix it, I'd be grateful.
Edit: changed which variable and line had a problem because I can't count or read.
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;
}
}
Upvotes: 1
Views: 617
Reputation: 1161
It's probably because firstbattlerealactualhealth
is defined inside an if
clause. If the program does not execute that if
block, firstbattlerealactualhealth
will not be defined. You have to define & initialize it somewhere that you are sure it will be executed.
Bottom line, if you define a variable inside a block({}
), it should only be used inside of that block, and nowhere else.
tips:
Don't name any of the variables first_sth
. It will most necessarily be followed by variables with names second_sth
and third_sth
and you will not be able to manage them. You should reuse that same logic for other battles (not only slimes) by passing different parameters, and that way you can write DRY(Don't repeat yourself) code. The variable names will also be much readable.
Upvotes: 2