While looping without obeying conditional

0

Good, I'm doing a code from two different classes (two different * .java elements), in the first ('ball') I set the attributes and methods:

public class Pelota {
 byte posX = 0;
 byte posY = 0;
 byte peso = 5;

     public Pelota(){
        this.posX = 0;
        this.posY = 0;
        this.peso = 5;}

 public void moverEnX() {
     this.posX = posX++;}
public void moverEnY(){
    this.posX = posY++;}}

To then open a separate class ('Main') from which to create a new ball object that moves a certain number of steps,

public class MainPelota {
public static void main (String [] args){
    Pelota objeto1 = new Pelota();
    System.out.println("la posicion inicial de objeto1 es: " + objeto1.posX +"\n Hay un foso en la casilla 8");

    objeto1.posX = 0;

    while (objeto1.posX<8){
        System.out.println("La pelota avanza, ahora está en la posición" + objeto1.posX);

    objeto1.moverEnX();

    if (objeto1.posX>=8){
        System.out.println("La pelota se ha caído");}
        }
    }
}

However, when executing it, all it does is print the statement of the While loop indefinitely, without 'posX' advancing, I suspect that the problem is when it comes to complying with the 'moverEnX' method but I do not understand why not it works if it is able to access 'posX' without problem.

    
asked by Pablo León 09.04.2016 в 11:24
source

2 answers

1

The problem is in the function moverEnX()

public void moverEnX() {
     this.posX = posX++;
}

When the ++ operator is to the right of the variable, it increases the value but returns the previous value that the variable had ... that is to say that if it is 5, it adds one and changes the value of the variable to 6, but being on the right, returns 5. Then the operator = sets the variable to 5, since it is what it receives.

In other words, posX changes to 6, but immediately changes back to 5.

Basically, it never leaves the loop, because from the point of view of the while, posX never changes.

Now, as posX++ already makes the assignment of the new value, there is no need to do so using the operator = . This would be enough:

public void moverEnX() {
     posX++;
}
    
answered by 09.04.2016 / 12:46
source
1
//Simplemente, cambia en los métodos eso y listo.

public class Pelota {

    byte posX;
    byte posY;
    byte peso;

     public Pelota(){
        this.posX = 0;
        this.posY = 0;
        this.peso = 5;
     }

    public void moverEnX() {
        posX++;

    }
    public void moverEnY(){
        posY++;
    }



    public static void main(String[] args) {
        Pelota objeto1 = new Pelota();
        System.out.println("la posicion inicial de objeto1 es: " + objeto1.posX +"\n Hay un foso en la casilla 8");


        while (objeto1.posX<8){
            System.out.println("La pelota avanza, ahora está en la posición: " + objeto1.posX);

            objeto1.moverEnX();

            if (objeto1.posX>=8){
                System.out.println("La pelota se ha caído");
            }
        }
    }
}
    
answered by 09.04.2016 в 12:05