Home > Software design >  Codes inside if else statement is not working Java
Codes inside if else statement is not working Java

Time:03-17

I only recently started learning the use of getters and setters and tried to implement them in a project. I think it is the problem why my if-else statement is working. Here is my code:

Hero person = new Hero("Ambut", 700, 40, 80);
Monster allMonster = new Monster(75, 125);
Monster vampire = new Monster("Vampire", 500);
int counter = 0;
    int monsterHealth, personHealth;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        heroName = findViewById(R.id.txtHeroName);
        heroHP = findViewById(R.id.txtHeroHealth);
        monsterName = findViewById(R.id.txtMonsterName);
        monsterHP = findViewById(R.id.txtMonsterHealth);
        heroName.setText(String.valueOf(person.getName()));
        heroHP.setText(String.valueOf(person.getHealth()));
        personHealth = person.getHealth();

    monsterName.setText(String.valueOf(vampire.getName()));
    monsterHP.setText(String.valueOf(vampire.getHealth()));
    monsterHealth = vampire.getHealth();
    personDPS();
    vampire.setHealth(monsterHealth);
    person.setHealth(personHealth);
    if (monsterHealth <= 0) {
    btn5.setVisibility(View.GONE);
    }
    else if (personHealth <= 0) {
    btn5.setVisibility(View.GONE);
}

The method personDPS subtracts the value in monsterHealth and personHealth alternately, everytime the button is clicked. It works well, but I do not want it to reach a negative number thus the if-else statement above. I am wondering if there is a problem with my logic or if I used encapsulation wrong which is why the codes inside the if-else statement is not working?

Also for reference method personDPS is:

public void personDPS() {
    btn5.setOnClickListener(view -> {
          counter;
        Random randomizer = new Random();
        if (counter % 2 == 1) {
            int personDmg = randomizer.nextInt(person.getMaxAtk() - person.getMinAtk())   person.getMinAtk();
            monsterHealth = monsterHealth - personDmg;
            monsterHP.setText(String.valueOf(monsterHealth));
        } else if (counter % 2 == 0) {
            int monsterDmg = randomizer.nextInt(allMonster.getMaxAtk() - allMonster.getMinAtk())   allMonster.getMinAtk();
            personHealth = personHealth - monsterDmg;
            heroHP.setText(String.valueOf(personHealth));
        }
    });

}

CodePudding user response:

It appears that you're missing a closing brace (curly brackets, "}") at the end of the else if. Here is what I think it should look like:

Hero person = new Hero("Ambut", 700, 40, 80);
Monster allMonster = new Monster(75, 125);
Monster vampire = new Monster("Vampire", 500);
int counter = 0;
int monsterHealth, personHealth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    heroName = findViewById(R.id.txtHeroName);
    heroHP = findViewById(R.id.txtHeroHealth);
    monsterName = findViewById(R.id.txtMonsterName);
    monsterHP = findViewById(R.id.txtMonsterHealth);
    heroName.setText(String.valueOf(person.getName()));
    heroHP.setText(String.valueOf(person.getHealth()));
    personHealth = person.getHealth();

    monsterName.setText(String.valueOf(vampire.getName()));
    monsterHP.setText(String.valueOf(vampire.getHealth()));
    monsterHealth = vampire.getHealth();
    personDPS();
    vampire.setHealth(monsterHealth);
    person.setHealth(personHealth);

    if (monsterHealth <= 0) {
        btn5.setVisibility(View.GONE);
    }
    else if (personHealth <= 0) {
        btn5.setVisibility(View.GONE);
    }
}

CodePudding user response:

Here is a simple solution for your problem, decrease the health only when it has greater than Zero, for me your method personDPS(); is not quite right, we can't decrease both monster and person's HP at once, but anyway, let's do this:

check personHealth and monsterHealth HP and then trigger the method personDPS() if the methods for decrementing HP are different then you can check with OR meaning someone might be dead already but the other is alive... like this, public int or float decrementMonisterHP(type, dmgDealt){...}

// GET both HP
personHealth = person.getHealth();
monsterHealth = vampire.getHealth();
// Check if we can call the decrement method again, the main problem for you as I 
if (personHealth > 0 && monsterHealth > 0){
    // subtract HP for BOTH
    personDPS();
}else{
// whether any of the BTN should be hidden.

    if (monsterHealth <= 0) {
        btn5.setVisibility(View.GONE);
    }else if (personHealth <= 0) {
        btn5.setVisibility(View.GONE);
    }
}

the better way is to have two different methods and call them like this:

monsterHealth = vampire.getHealth();
if (monsterHealth <= 0) {
    btn5.setVisibility(View.GONE);
}else{
    decrementMonisterHP(...);
}

personHealth = person.getHealth();
if (personHealth <= 0) {
    btn5.setVisibility(View.GONE);
}else{
    decrementPersonHP(...);
}

if your project is OOP structure you can do something like this.

decrementHP(type, dmgDealt)

personObj.decrementHP(dmgDealt)

monisterObj.decrementHP(dmgDealt)

Please let me know if you still need help

  • Related