Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Commander01 en 9 Mayo 2019, 03:13 am



Título: ayudenme porfa soy nuevo
Publicado por: Commander01 en 9 Mayo 2019, 03:13 am
Si X es una variable float con valor 85.3, determina qué se muestra por pantalla cuando se ejecutan las siguientes instrucciones:

float X = 85.3;

System.out.println(x);

x++;

System.out.println(x);

System.out.println(++x);

System.out.println(x++);

System.out.println(x);

System.out.println(x++);

System.out.println(++x);

System.out.println(++x);

++x;

x++;

System.out.println(++x);

System.out.println(x++);

System.out.println(++x);


Título: Re: ayudenme porfa soy nuevo
Publicado por: EdePC en 9 Mayo 2019, 05:32 am
Saludos,

- Siendo muy sencillos en la explicación, el operador de pre-incremento (++x) primero incrementa x luego hace las demás operaciones en línea, el operador de post-incremento (x++) primero hace las operaciones en línea y luego incrementa x.

- Tener en cuenta que las "operaciones en línea" terminan en punto y coma ( ; )

- Con ejecutarlo te das cuenta:

Código
  1. class IncOperator {
  2.  public static void main(String[] args) {
  3.    float x = 85.3f;
  4.    System.out.println(x);     // Muestra: 85.3 | Luego x vale 85.3
  5.    x++;                       //               | Luego x vale 86.3
  6.    System.out.println(x);     // Muestra: 86.3 | Luego x vale 86.3
  7.    System.out.println(++x);   // Muestra: 87.3 | Luego x vale 87.3
  8.    System.out.println(x++);   // Muestra: 87.3 | Luego x vale 88.3
  9.    System.out.println(x);     // Muestra: 88.3 | Luego x vale 88.3
  10.    System.out.println(x++);   // Muestra: 88.3 | Luego x vale 89.3
  11.    System.out.println(++x);   // Muestra: 90.3 | Luego x vale 90.3
  12.    System.out.println(++x);   // Muestra: 91.3 | Luego x vale 91.3
  13.    ++x;                       //               | Luego x vale 92.3
  14.    x++;                       //               | Luego x vale 93.3
  15.    System.out.println(++x);   // Muestra: 94.3 | Luego x vale 94.3
  16.    System.out.println(x++);   // Muestra: 94.3 | Luego x vale 95.3
  17.    System.out.println(++x);   // Muestra: 96.3 | Luego x vale 96.3
  18.  }
  19. }