1

I'm trying to set up the first fight, but at line 24, BlueJ tells me that variable firsthitpoints is not visible to carry out.

I'd be grateful if someone could tell me what's wrong with this code

  if (characterquality == "Slayer"){
        int HP = 150;
        int Attack = 75;
        int Defense = 50;
        int MP = 20;
        int EXP = 0;
        int Gold = 50;

       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 = (int) Math.ceil(Math.random() * 50);

          int firstenemyhitpoints = (int) Math.ceil(Math.random() * 25);
          boolean firstkill = true;
          while (firstkill) {
              if (firstattack > firstenemydefense){
                  int firsthitpoints = (int) Math.ceil(Math.random() * firstbattleattack);

              }
              int firstenemynewhealth = firstenemyhealth - firsthitpoints;

              if (firstenemynewhealth <= 0){
                  firstkill = false;
              }
              if (firstattack <= firstenemydefense){
                 System.out.println("Attack failed!");
              }
              if (firstenemyattack > firstdefense){
                  int firstenemyhitpointattack = (int) Math.ceil(Math.random() * firstenemyhitpoints);
                  System.out.println("The enemy did " + firstenemyhitpointattack + " damage to you. You have " + firstbattlehealth + " health remaining.");
               }
              if (firstenemyattack <= firstdefense){
                  System.out.println("Enemy attack missed!");
              }
          }
          int firstenemynewhealth = firstenemynewhealth;
          if (firstenemynewhealth <= 0){
              firstbattle = false;
           }
       }
    }
  • 1
    Where is "firstattackhitpoints" inyour code? – Abdelhak Jan 23 '16 at 13:30
  • Line 24 is where is the error pops up. –  Jan 23 '16 at 13:32
  • 1
    where is line 24? tell us which – Abdelhak Jan 23 '16 at 13:32
  • Sorry for the amount of code around it, I just figured it might make more sense if I included more code to get the whole picture. Also, I'm sorry for the wacko variable names, but it's a bit easier for me to keep track of it. –  Jan 23 '16 at 13:33
  • Line 24 is 2 lines underneath the line that says "while (firstkill) {". It's also 8 lines up from the bottom if you don't scroll down the code, and 24 lines down from the top of the code block. –  Jan 23 '16 at 13:35
  • EDIT: Wrong variable name, was changed to "firsthitpoints". I'm sorry. –  Jan 23 '16 at 13:40

2 Answers2

2

You're defining the variable in the preceding if block, move it outside the block and give it an initial value. Something like,

boolean firstkill = true;
int firsthitpoints = 0; // <-- declare and initialize.
while (firstkill) {
    if (firstattack > firstenemydefense){
        firsthitpoints = Math.ceil(Math.random() * firstbattleattack);
    }
    int firstenemynewhealth = firstenemyhealth - firsthitpoints;
    // ...

Also, and not directly to your question, please don't compare String equality with ==.

if (characterquality == "Slayer"){

should be

if (characterquality.equals("Slayer")) {

Perhaps it works in your case, but that is only testing reference equality (not value equality). See also, How do I compare strings in Java?

Finally, there is a cost1 to executing a compare instruction. You should be using else blocks, instead of comparing logical inversions with multiple if(s). Something like,

if (firstenemyattack > firstdefense) { 
    int firstenemyhitpointattack = (int) Math.ceil(Math.random() 
            * firstenemyhitpoints);
    // Presumably, firstbattlehealth -= firstenemyhitpointattack here?
    System.out.println("The enemy did " + firstenemyhitpointattack 
            + " damage to you. You have " + firstbattlehealth 
            + " health remaining.");
} else { // <-- An "else" block, firstenemyattack <= firstdefense
    System.out.println("Enemy attack missed!");
}

1Albeit a very small one in a modern context.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Try to create a local variable firsthitpoints and initialize it like this:

    int firsthitpoints = 0;
          while (firstkill) {
              if (firstattack > firstenemydefense){
                   firsthitpoints = (int) Math.ceil(Math.random() * firstbattleattack);
Abdelhak
  • 8,299
  • 4
  • 22
  • 36