Roke98

Ejercicio4-Tp2-Unju

Sep 27th, 2022 (edited)
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package Practico2;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Ejercicio4 {
  7.     static Random aleatorio = new Random();
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         menu();
  11.     }
  12.    
  13.     public static void menu() {
  14.         int valor, opcion, seguir;
  15.         do {
  16.             System.out.println("1 - Ingresar Un valor");
  17.             System.out.println("2 - Generar un valor");
  18.             opcion = Helper.getPositiveInt("Ingrese una opcion");
  19.            
  20.             switch(opcion) {
  21.             case 1:
  22.                 valor = Helper.getPositiveInt("ingrese un valor a convertir");
  23.                 menu2(valor);
  24.                
  25.                 break;
  26.             case 2:
  27.                 valor = aleatorio.nextInt(1000);
  28.                 menu2(valor);
  29.                
  30.                 break;
  31.             default:
  32.                 System.out.println("Opcion no valida");
  33.             }
  34.             seguir = Helper.getPositiveInt("Desea ingresar otro valor??(1=Si//2=No)");
  35.         }while(seguir!=2);
  36.         System.out.println("Gracias :D");
  37.     }
  38.    
  39.     public static void menu2(int valor) {
  40.         Stack<Integer> resto = new Stack<Integer>();
  41.         int opcion2;
  42.         do {
  43.             System.out.println("1 - Pasar el valor a Base Binario");
  44.             System.out.println("2 - Pasar el valor a Base Octal");
  45.             System.out.println("3 - Regresar");
  46.             opcion2 =Helper.getPositiveInt("En que sistema desea convertir");
  47.  
  48.                 switch(opcion2) {
  49.                 case 1:
  50.                     System.out.println("el valor Original es " + valor);
  51.                     System.out.print("Su equivalencia en Binario es ");
  52.                     binario(resto, valor);                 
  53.                     System.out.println();
  54.                     espera();
  55.                    
  56.                     break;
  57.                 case 2:
  58.                     System.out.println("el valor Original es " + valor);
  59.                     System.out.print("Su equivalencia en Octal es ");
  60.                     octal(resto, valor);                   
  61.                     System.out.println();
  62.                     espera();
  63.                    
  64.                     break;
  65.                 case 3:
  66.                     break;
  67.                 default:
  68.                     System.out.println("opcion no valida");
  69.                 }
  70.         }while(opcion2 != 3);
  71.        
  72.     }
  73.    
  74.     public static void binario(Stack <Integer> pila, int valor) {
  75.         int resto;
  76.         while(valor > 0) {
  77.             resto = valor % 2;
  78.             pila.push(resto);
  79.             valor= valor/2;
  80.         }
  81.         while(!pila.empty()) {
  82.             System.out.print(pila.pop());
  83.         }
  84.     }
  85.    
  86.     public static void octal(Stack <Integer> pila, int valor) {
  87.         int resto;
  88.         while(valor > 0) {
  89.             resto  = valor % 8;
  90.             pila.push(resto);
  91.             valor = valor/8;
  92.         }
  93.         while(!pila.empty()){
  94.             System.out.print(pila.pop());
  95.         }
  96.     }
  97.  
  98.     public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
  99.         Scanner waitForKeypress = new Scanner(System.in);
  100.         System.out.print("Presiona la tecla Enter para continuar....");
  101.         waitForKeypress.nextLine();
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment