Created
May 14, 2012 20:51
-
-
Save godie007/2696812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package trabajo; | |
| import java.io.*; | |
| import javax.swing.*; | |
| public class Proyecto { | |
| // el metodo main inicia | |
| public static void main(String arg[]) throws IOException { | |
| String NumADividir; | |
| String Resultado = ""; | |
| int bin[] = new int[100]; | |
| int size = 100, posStart = -1; | |
| int x = 0, temp; | |
| NumADividir = JOptionPane | |
| .showInputDialog("Número entero a convertir ? "); | |
| // verifica que la cadena no sea mayor a 8 caracteres | |
| if (NumADividir.length() <= 8) { | |
| // inicializa vector | |
| for (x = 0; x < size; x++) | |
| bin[x] = -1; | |
| temp = Integer.parseInt(NumADividir); | |
| bin[0] = temp % 2; | |
| // toma los binarios | |
| for (x = 1; x < size; x++) { | |
| temp /= 2; | |
| bin[x] = temp % 2; | |
| } | |
| // recorta para no tomar los ceros excedentes | |
| for (x = size - 1; x >= 0; x--) | |
| if (bin[x] == 1) { | |
| posStart = x; | |
| break; | |
| } | |
| // forma la cadena final | |
| for (x = posStart; x >= 0; x--) | |
| Resultado = Resultado + Integer.toString(bin[x]); | |
| JOptionPane.showMessageDialog(null, "Binario: " + Resultado, | |
| "Resultados", JOptionPane.PLAIN_MESSAGE); | |
| } else { | |
| JOptionPane.showMessageDialog(null, | |
| "La cantidad contiene más de 8 digitos", "Error !!!!", | |
| JOptionPane.PLAIN_MESSAGE); | |
| } | |
| System.exit(0); // terminar aplicacion de la ventana | |
| } // fin del metodo main | |
| } // fin de la clase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment