Take a value from a method

0

Good Companions, I have a problem. I have the following method:

public void repartirPC1() {
        try {
            CartasPc1[0] = (int) (Math.random() * 13);
            CantidadCartas[CartasPc1[0]]--;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Problemas al ejecutar revisar 1");
        }

        try {
            CartasPc1[1] = (int) (Math.random() * 13);
            CantidadCartas[CartasPc1[0]]--;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Problemas al ejecutar revisar 2");
        }

        System.out.println(" ");
        System.out.println("PC 1 tiene: ");

        //System.out.println("test: "+ valorCartas[2]);
        //Interpretar el valor de las cartas.
        int num1, num2; // variables para poder enmascarar el valor de las cartas y efectuar la suma
        num1 = CartasPc1[0];
        num2 = CartasPc1[1];
        //Asignacion de los valores de las cartas mediante un variable temporal
        tempSume += getCart(num1); // Brinda el numero de la carta
        tempSume += getCart(num2); // Brinda el numero de la carta
        Suma = tempSume;
        // System.out.println("Numeros del arreglo " + num1 + " , " + num2); // Para conocer la posicion del arreglo a la hora de imprimir
        System.out.println(" ");
        System.out.println("Para su Juego tiene un total de: " + Suma);

    }

and I need to extract the value of the sum to compare it in another method.

public void demostracion() {
        int suma1=0, suma2=0;
        PC p = new PC();
        p.repartirPC1();

        this.tempSume = suma1;

        System.out.println(" temporal 1  " + this.tempSume);
        System.out.println(" temporal  2 " + suma1);

        System.out.println(" temporal  3 " + getSuma());

But I always give zero the value, I already declare it as a global variable and I put Get and set to be able to get the values but I do not get how to get the value. I appreciate the help.

    
asked by Felix mejias 26.07.2018 в 04:10
source

1 answer

0

Although I can not see all your code, I'm almost sure it's a reference problem, if you need to accumulate the sums, declare "sum" as "static". If you do this automatically the variable is of the class and there is only one instance in memory at the time of execution (It does not matter if you create more objects of the same class).

public static int suma;
    
answered by 26.07.2018 в 04:22