Home > database >  If else statement (java)
If else statement (java)

Time:08-20

I just saw this problem on youtube. How to use if else boolean in java Public fight method calls hitAttempt method. If hitAttempt method returns true, then it will call the hitDamage method. The damage will be display.

Heres the code

public class Goodman {

// PRIVATE PROPERTIES ::::::::::::::::::::::::::::::::>

private int strength;
private int speed;
private int health;



//PUBLIC PROPERTIES :::::::::::::::::::::::::::::::::>

public String name;


// GETTERS ::::::::::::::::::::::::::::::::::>

public int getStrength() {
  return strength;
}

public int getSpeed(){
  return speed;
}

public int getHealth(){
  return  health;
}


//CONSTRUCTORS ::::::::::::::::::::::::::::::::::::>
public Goodman (String name) {
  this.name = name;
 
  generateAbilities();
}


public void showPowers() {
  System.out.println("<::::::::::::::::::::::>");
  System.out.println("Strength:  "   strength);
  System.out.println("Speed:     "   speed);
  System.out.println("Health:    "   health);
  System.out.println("<::::::::::::::::::::::>");
  
}



// PRIVATE METHODS ::::::::::::::::::::::::::::::>
private void generateAbilities(){

 this.strength = (int)(Math.random()*100 1);
 this.speed = (int)(Math.random()*100 1);
 this.health = (int)(Math.random()*100 1);     
 this.damage = (int)(Math.random()*100 1);
        
}




// PUBLIC METHODS :::::::::::::::::::::::::::::::>

public void fight() {
  
  System.out.println(this.name   "is fighting");
  
  
}

public boolean hitAttempt() {

}

public void hitDamage(){

}

}

CodePudding user response:

Like you said if hitAttempt() returns true then call hitDamage().

In if condition you don't need to explicity check if (condition == true), only if (condition) will suffice.

if (hitAttempt()) {
            hitDamage();
        }
  • Related