Search code examples
javabluej

BlueJ cannot find a variable


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;
           }
       }
    }

Solution

  • 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.