Failure to update variables

Asked

Viewed 34 times

-1

I am developing a project in java, a "farm game" where there are several Tats for the character that is created. When we create a character defenimos which his class, it will influence his Stats.

My problem is here, when I choose the class the Stats do not update properly.

Code for choosing the class:

import java.util.InputMismatchException;

public class Knight extends Game {

    public static PlayerClass chooseSubClass() {
        System.out.println("1 -» Heavy \n2 -» SwordsMaster \n3 -» Royal");
        int op = 0;
        while (op == 0) {
            try {
                op = Main.in.nextInt();
            } catch (InputMismatchException ex) {
                System.out.println(ex);
            }
        }
        PlayerClass playerClass;
        switch (op) {
            case 1:
                playerClass = new Heavy();
                break;
            case 2:
                playerClass = new Swordsmaster();
                break;
            case 3:
                playerClass = new Royal ();
                break;
            default:
                playerClass = chooseSubClass();
                break;
        }
        return playerClass;
    }
}

If heavy choice:

public class Heavy extends PlayerClass {

    public Heavy() {
        attackDamage += 50;
        armorPenetration += 20;
    }
}

The Tats:

public class PlayerClass {

    public long attackDamage = 10;
    public long armorPenetration;
    public long magicDamage= 10;
    public long magicPenetration;
    public double attackSpeed= 1;
    public long money = 100;
    public int level = 1;



}

Player code

public class Player extends PlayerClass{


    private String playerName;
    private PlayerClass playerClass;

    public String classes = "1 -» Knight \n2 -» Mage \n3 -» Archer";

    //Chooses the class and subclass of the player
    public void chooseClass() {

        System.out.println(classes);
        int op = Main.in.nextInt();
        switch (op) {
            case 1:
                playerClass = Knight.chooseSubClass();
                break;
            case 2:
                playerClass = Mage.chooseSubClass();
                break;
            default:
                chooseClass();
                break;
        }
    }


    // Setter method
    public void setPlayerName(String name) {
        playerName = name;
    }

    // Getter method
    public String getPlayerName() {
        return playerName;
    }
    public String getClassName() {
        return playerClass.getClass().getName();
    }

}

1 answer

0


I analyzed your code quickly and as far as I could tell the problem lies in the fact that you use it armorPenetration += 20 without its variable armorPenetration has an initial value. In this case you must choose a default value in the creation of your class, in this case Playerclass. For example, choosing 0 as the starting value would look like this class:

public class PlayerClass {

public long attackDamage = 10;
public long armorPenetration = 0;
public long magicDamage= 10;
public long magicPenetration = 0;
public double attackSpeed= 1;
public long money = 100;
public int level = 1;

}

In these cases it is always good to start with a default value when creating a new class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.