Returns incorrect value JAVA

0

I mention my doubt, beforehand saying that I am starting with Java and I am practicing with the methods, I come from another language also oriented to objects but also to very low level.

My question or mistake is the following, I am, as a hobby, creating an application that serves me to generate characters tokens for a specific RPG, this has to do several random dice rolls, well, in one of the classes (sorry if I'm wrong in the terms) I have designed a series of queries with conditional "if" nested to solve the type of character you want to play, previously you receive the parameters and create an array, good in this case two, to order from greater to less the values and distribute them according to interest more to the final result of the pj. Throughout the SETTER method, the values are managed correctly, but in each of the GETTER methods I can not get the correct value back and I simply return an int variable = 0;

I detail the code of the class in question

public class Profesion {

private int fue, con, des, pod, car, tam, inte;
private String prof;

public Profesion() {

}

public int getFue() {
    return fue;
}

public int getCon() {
    return con;
}

public int getDes() {
    return des;
}

public int getPod() {
    return pod;
}

public int getCar() {
    return car;
}

public int getTam() {
    return tam;
}

public int getInte() {
    return inte;
}

public void setProf(int carTemp1, int carTemp2, int carTemp3, int carTemp4, int carTemp5,
    int carTemp6, int carTemp7) {

    int [] orden3d6 = {carTemp1, carTemp2, carTemp3, carTemp4, carTemp5};
    int [] orden2d6 = {carTemp6, carTemp7};

    Arrays.sort(orden3d6);
    Arrays.sort(orden2d6);

    prof = (String) JOptionPane.showInputDialog(null, "Cuál Profesión tendra el personaje?",
            "Example1", JOptionPane.QUESTION_MESSAGE, null, new Object[] { "Adiestrador de animales", 
                    "Agente", "Alquimista", "Artesano", "Artista","Cazador", "Chaman", "Cortesana", 
                    "Cortesana", "Cortesano(burgués)", "Erudito", "Explorador", "Funcionario", "Granjero",
                    "Guerrero", "Hechicero", "Ladrón", "Marinero", "Médico", "Mercader", "Minero",
                    "Místico", "Pastor", "Pescador"}, "Guerrero");

    if (prof.equals("Adiestrador de animales")) {
        String resp1 = (String) JOptionPane.showInputDialog(null, "Que tipo de Adiestrador?", "Example1", 
            JOptionPane.QUESTION_MESSAGE,null, new Object[] {"Luchador", "Instructor"}, "Instructor");
            if (resp1.equals("Luchador")) {
                fue = orden3d6[3];  
                con = orden3d6[1];
                des = orden3d6[0];
                pod = orden3d6[4];      
                car = orden3d6[2];      
                tam = orden2d6[0];
                inte = orden2d6[1];
            }else if(resp1.equals("Instructor")) {
                fue = orden3d6[0];
                con = orden3d6[1];
                des = orden3d6[2];
                pod = orden3d6[4];      
                car = orden3d6[3];      
                tam = orden2d6[0];
                inte = orden2d6[1];
            }else {
                System.out.println ("Respuesta no válida");
            }


        }

        if (prof.equals("Guerrero")) {
            String resp1 = (String) JOptionPane.showInputDialog(null, "Que tipo de guerrero?", "Example1", 
                    JOptionPane.QUESTION_MESSAGE,null, new Object[] {"Tanque", "Ágil", "Equilibrado"}, "Equilibrado");
            if (resp1.equals("Equilibrado")) {
                fue = orden3d6[4];
                con = orden3d6[3];
                des = orden3d6[2];
                pod = orden3d6[0];      
                car = orden3d6[1];      
                tam = orden2d6[1];
                inte = orden2d6[0];
            }else if(resp1.equals("Ágil")) {
                fue = orden3d6[2];
                con = orden3d6[3];
                des = orden3d6[4];
                pod = orden3d6[0];      
                car = orden3d6[1];      
                tam = orden2d6[0];
                inte = orden2d6[1];
            }else if(resp1.equals("Tanque")) {
                fue = orden3d6[2];
                con = orden3d6[4];
                des = orden3d6[3];
                pod = orden3d6[0];      
                car = orden3d6[1];      
                tam = orden2d6[1];
                inte = orden2d6[0];
            }else {
                System.out.println ("Respuesta no válida");
            }
        }


    }

}

I add the main class:

package PJ;

public class Crear_PJ {

public static void main(String[] args) {

    Caracteristicas car1 = new Caracteristicas();
    Caracteristicas car2 = new Caracteristicas();
    Caracteristicas car3 = new Caracteristicas();
    Caracteristicas car4 = new Caracteristicas();
    Caracteristicas car5 = new Caracteristicas();
    Caracteristicas car6 = new Caracteristicas();
    Caracteristicas car7 = new Caracteristicas();

    Atributos pA  = new Atributos(); 
    Atributos mD = new Atributos();

    Profesion orden = new Profesion();
    Profesion fue = new Profesion();
    Profesion con = new Profesion();
    Profesion des = new Profesion();
    Profesion pod = new Profesion();
    Profesion car = new Profesion();
    Profesion tam = new Profesion();
    Profesion inte = new Profesion();


    car1.setCAR_3d6();
    car2.setCAR_3d6();
    car3.setCAR_3d6();
    car4.setCAR_3d6();
    car5.setCAR_3d6();
    car6.setCAR_2d6();
    car7.setCAR_2d6();

    int carTemp1 = car1.getCAR_3d6();
    int carTemp2 = car2.getCAR_3d6();
    int carTemp3 = car3.getCAR_3d6();
    int carTemp4 = car4.getCAR_3d6();
    int carTemp5 = car5.getCAR_3d6();
    int carTemp6 = car6.getCAR_2d6();
    int carTemp7 = car7.getCAR_2d6();


    orden.setProf(carTemp1, carTemp2, carTemp3, carTemp4, carTemp5, carTemp6, carTemp7);


    int fuE = fue.getFue();
    int coN = con.getCon();
    int deS = des.getDes();
    int poD = pod.getPod();
    int caR = car.getCar();
    int taM = tam.getTam();
    int inTe = inte.getInte();

    mD.setMD(fuE, taM);
    pA.setPA(deS, inTe);


    System.out.println("La FUE es " + fuE);
    System.out.println("La CON es " + coN);
    System.out.println("El TAM es " + taM);
    System.out.println("El DES es " + deS);
    System.out.println("El INT es " + inTe);
    System.out.println("El POD es " + poD);
    System.out.println("El CAR es " + caR);
    System.out.println("Los Puntos de Acción son " + pA.getPA());
    System.out.println("El modificaro de daño es " + mD.getMD());
}

}

And the Attributes class:

package PJ;

public class Attributes {

private int pA;
private int ac;
private int mD;
private String daño;

public Atributos() {
    ac = 0;
    daño = "";
}

public int getPA() {
    return ac;
}

public String getMD() {
    return daño;
}


public void setPA(int des, int inte ) {

    pA = des + inte;
    if (pA<13) {
        ac = 1;
    }else if (pA>12 && pA<25) {
        ac = 2;
    }else if (pA>25 && pA<37) {
        ac = 3;
    }else if (pA>36 && pA<49) {
        ac = 4;
    }else if (pA>49) {
        ac = 5;
    }else ac = 0;


}


public void setMD(int fue, int tam) {

    mD = fue + tam;

    if (mD<6) {
        daño = "-1d8";
    }else if (mD>7 && mD<11) {
        daño = "-1d6";
    }else if (mD>7 && mD<11) {
        daño = "-1d6";
    }else if (mD>10 && mD<16) {
        daño = "-1d4";
    }else if (mD>15 && mD<21) {
        daño = "-1d2";
    }else if (mD>20 && mD<26) {
        daño = "0";
    }else if (mD>25 && mD<31) {
        daño = "+1d2";
    }else if (mD>30 && mD<36) {
        daño = "+1d4";
    }else if (mD>35 && mD<41) {
        daño = "+1d6";
    }else if (mD>40 && mD<46) {
        daño = "+1d8";
    }else if (mD>45 && mD<51) {
        daño = "+1d10";
    }else if (mD>50 && mD<61) {
        daño = "+1d12";
    }else if (mD>70 && mD<81) {
        daño = "+2d6";
    }else if (mD>80 && mD<91) {
        daño = "+2d8";
    }else if (mD>90 && mD<101) {
        daño = "+1d10+1d8";
    }else if (mD>100 && mD<111) {
        daño = "+2d10";
    }else if (mD>110 && mD<121) {
        daño = "+2d10+1d2";
    }else if (mD>120 && mD<131) {
        daño = "+2d10+1d4";
    }else daño = "Consulta tabla pag 13";



}

}

I add the initial roll of dice:

package PJ;

class Features {

private double dado3d6_1, dado3d6_2, dado3d6_3;
private int valor;

public Caracteristicas(){valor = 0;}

public int getCAR_3d6() {return valor;}

public int getCAR_2d6() {return valor;}

public void setCAR_3d6() {

    do {
        dado3d6_1=Math.random()*6;
    } while (dado3d6_1 < 1);

    do {
        dado3d6_2=Math.random()*6;
    } while (dado3d6_2 < 1);

    do {
        dado3d6_3=Math.random()*6;
    } while (dado3d6_3 < 1);

    valor = (int)(dado3d6_1+dado3d6_2+dado3d6_3);

}

public void setCAR_2d6() {

    do {
        dado3d6_1=Math.random()*6;
    } while (dado3d6_1 < 1);

    do {
        dado3d6_2=Math.random()*6;
    } while (dado3d6_2 < 1);

    valor = (int)(dado3d6_1+dado3d6_2+6);
}

}

    
asked by guacanaqui 31.12.2018 в 19:35
source

1 answer

0

You do not need to do this

 Profesion orden = new Profesion();
    Profesion fue = new Profesion();
    Profesion con = new Profesion();
    Profesion des = new Profesion();
    Profesion pod = new Profesion();
    Profesion car = new Profesion();
    Profesion tam = new Profesion();
    Profesion inte = new Profesion();

You are generating a different instance each time you call

Profesion x = new Profesion();

For that instance only once and use the setters and getters of that class for example

Profesion profesion = new Profesion();

Setting values

profesion.setFue(20);

getting them

profesion.getFue();

The same as with this

Caracteristicas car1 = new Caracteristicas();
    Caracteristicas car2 = new Caracteristicas();
    Caracteristicas car3 = new Caracteristicas();
    Caracteristicas car4 = new Caracteristicas();
    Caracteristicas car5 = new Caracteristicas();
    Caracteristicas car6 = new Caracteristicas();
    Caracteristicas car7 = new Caracteristicas();

instantiate it as a profession, only once

 Caracteristicas car = new Caracteristicas();
    
answered by 31.12.2018 / 20:41
source