Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Problem with if/else inside a contructor

  1. #1
    TheSmith is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Question Problem with if/else inside a contructor

    First, hi everyone I'm new here. Just started learning Java.
    So here is the problem, I'm trying to make a program where the object "takes damage" and when health points reachs zero a boolean created to check if he is alive or not, would become false. And that is not happening =(

    Java Code:
    public class Init {
    	public static void main(String args[]){
    		
    		
    		Inimigo cobra = new Inimigo(10,true,0);
    		System.out.println(cobra.health);
    		
    		cobra.dano = 2;
    		cobra.health = cobra.health - cobra.dano; //(10-8) cobra.health becomes equal to 8
    		System.out.println(cobra.health);
    		System.out.println(cobra.estaVivo); // stays true
    		
    		cobra.dano = 8;
    		cobra.health = cobra.health - cobra.dano; //(8-8) cobra.health = 0
    		System.out.println(cobra.health);
    		System.out.println(cobra.estaVivo); // still true for some reason
    		
    	
    	}
    }
    Java Code:
    public class Inimigo {
    
    	int health;
    	boolean estaVivo;
    	int dano;
    	Inimigo(int hp, boolean vivo, int dmg){
    		health = hp;
    		dano = dmg;
    		estaVivo = vivo;
    		
    		if(health <= 0){	
    			health = health - dano;
    			estaVivo = false;
    			
    		}
    	}
    }
    This is the result
    10
    8
    true
    0
    true
    The wrong thing is the last true. I tried different ways but the results where still the same or the opposite, both of them become false.
    Thx in advance ;D

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Problem with if/else inside a contructor

    Having the if statement in the constructor will only be executed when you construct the object.

    So if you pass health <= 0 estavivo will be false forever, in your case true. You have to change it after.

    My advice would be to make methods for every action instead of using the variables directly.

  3. #3
    TheSmith is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with if/else inside a contructor

    Oh, I didn't know that. Thanks for that. I will try. ;D

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Problem with if/else inside a contructor

    If you're defining vivo as health > 0, the boolean is redundant. You could just have a isVivo() method that returns a value based on health:

    Java Code:
    public boolean isVivo() {
       return health > 0;
    }
    gimbal2 likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 0
    Last Post: 11-07-2012, 12:44 PM
  2. Problem inside of event handler
    By mwr1976 in forum New To Java
    Replies: 1
    Last Post: 11-12-2011, 04:11 AM
  3. SWT CCombo inside TableViewer problem
    By mrshawnwu in forum SWT / JFace
    Replies: 0
    Last Post: 04-15-2010, 06:37 PM
  4. cannot find symbol contructor
    By blessed07 in forum Reviews / Advertising
    Replies: 3
    Last Post: 01-19-2010, 11:34 AM
  5. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •