Jump to content

Java help


Go to solution Solved by Guest,

5 posts in this topic

Recommended Posts

  • Solution
Posted

public void setHealth(int newHealth){
    if (hp <= 100){
    this.hp = newHealth;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {
}

    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);

Why the heck is your parameter name the same as your instance variable name

 

class Player{

private int hp; //player health

public void setHealth(int hp){
    if (hp <= 100){
    this.hp = hp;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {


    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);
}
}
}
Each time I try to print out the private in hp it = 0 and not 100 as assigned in the value I assigned it in the main field.
Posted
public void setHealth(int newHealth){
    if (hp <= 100){
    this.hp = newHealth;
    }
    else{
    System.out.println("Health can't procced over 100");
    }

public int getHp(){return hp;}

public class JavaApplication19 {
}

    public static void main(String[] args) {

        int health = 100;
        Player condition = new Player();
        condition.setHealth(health);
Why the heck is your parameter name the same as your instance variable name

 

cause thats what a tutorial told me to do

Posted

cause thats what a tutorial told me to do

What a horrible tutorial what the f**k thats awful. What its telling you to do is to write code to set a variable to itself

Posted

What a horrible tutorial what the f**k thats awful. What its telling you to do is to write code to set a variable to itself

I'm trying to use the private hp in a different class and tried using getters and setters to accomplish this and looked into a youtube video so I can understand it but... that happened

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...

Important Information

We would like to place cookies on your device to help make this website better. The website cannot give you the best user experience without cookies. You can accept or decline our cookies. You may also adjust your cookie settings. Privacy Policy - Guidelines