Problem with do..while cycle that is not repeated properly

0

I have developed this program and it works well, however it does not coordinate the cycle Do...While
That is, I need that once given (the number inverted), I continue to print the same question, in order to keep repeating the cycle, until finally when you assign the value 0, the program ends correctly.

import javax.swing.JOptionPane;
public class MetodoParaInvertir {
    public static void main(String[] args) {

    int num, n, resultado=0;

    num= Integer.parseInt(JOptionPane.showInputDialog("Ingrese un numero entero: ")); 
    System.out.println ("El numero ingresado es : "+num);

    do {
    n = num % 10; 
    num = num / 10;                                       
    resultado = resultado * 10 + n;


    } while (num != 0);
    System.out.println (" \nY su reves es: " +resultado); 

    if ( resultado >0 ) {                                

    num= Integer.parseInt(JOptionPane.showInputDialog("Ingrese Nuevamente Un Numero: ")); 
    System.out.println ("El numero ingresado es : "+num);
    System.out.println (" \nSu reves es: " +resultado); 

    }   
    if (resultado <0) {
    System.out.println (" \nFin del ciclo! ");
    }   
}
   }
    
asked by Julian Andres Micolta 22.04.2018 в 22:51
source

2 answers

0

You should not work with the number entered directly, you should store it in a separate variable

falta = num;

The loop should be like this

do{
    n = falta % 10;
    resultado = resultado * 10 + resto;
    falta = falta / 10;
}while(falta != 0);
    
answered by 22.04.2018 в 22:58
0

To start I do not know what you do using System.out.println() when class JOptionPane has the showMessageDialog() method that allows you to display the data entered by showInputDialog() .

Secondly, as I understand your question, you want that if the user enters the number 0 your program is closed and if not, keep asking for a number. For this, just use a general do-while .

import javax.swing.JOptionPane;

public class MetodoParaInvertir
{
  public static void main(String[] args)
  {
    int numero, auxiliar, resto, resultado;

    do {
      numero = Integer.parseInt(JOptionPane.showInputDialog("Ingrese un número entero: ")); //numero en lugar de num
      JOptionPane.showMessageDialog(null, "El número ingresado es: " + numero);

      if (numero == 0)
      {
        JOptionPane.showMessageDialog(null, "Fin del ciclo.");
      }
      else
      {
        auxiliar = numero;
        resultado = 0; //aquí reseteamos el valor de resultado

        while (auxiliar != 0) //cambié el do-while por un while
        {
          resto = auxiliar % 10; //resto en vez de n
          resultado = resultado * 10 + resto;
          auxiliar = auxiliar / 10;
        }
        JOptionPane.showMessageDialog(null, "El revés del número ingresado es: " + resultado);
      }
    } while (numero != 0);
  }
}

I have added the variable auxiliar so that it is with this variable that the calculations are carried out to find the inverted number of the original number. Otherwise, if we work with the variable numero , this will end up being equal to 0 and so the condition numero != 0 at the end of do-while will always be false.

    
answered by 23.04.2018 в 03:54