Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practico2;
- import java.util.Random;
- import java.util.Scanner;
- public class Ejercicio4 {
- static Random aleatorio = new Random();
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- menu();
- }
- public static void menu() {
- int valor, opcion, seguir;
- do {
- System.out.println("1 - Ingresar Un valor");
- System.out.println("2 - Generar un valor");
- opcion = Helper.getPositiveInt("Ingrese una opcion");
- switch(opcion) {
- case 1:
- valor = Helper.getPositiveInt("ingrese un valor a convertir");
- menu2(valor);
- break;
- case 2:
- valor = aleatorio.nextInt(1000);
- menu2(valor);
- break;
- default:
- System.out.println("Opcion no valida");
- }
- seguir = Helper.getPositiveInt("Desea ingresar otro valor??(1=Si//2=No)");
- }while(seguir!=2);
- System.out.println("Gracias :D");
- }
- public static void menu2(int valor) {
- Stack<Integer> resto = new Stack<Integer>();
- int opcion2;
- do {
- System.out.println("1 - Pasar el valor a Base Binario");
- System.out.println("2 - Pasar el valor a Base Octal");
- System.out.println("3 - Regresar");
- opcion2 =Helper.getPositiveInt("En que sistema desea convertir");
- switch(opcion2) {
- case 1:
- System.out.println("el valor Original es " + valor);
- System.out.print("Su equivalencia en Binario es ");
- binario(resto, valor);
- System.out.println();
- espera();
- break;
- case 2:
- System.out.println("el valor Original es " + valor);
- System.out.print("Su equivalencia en Octal es ");
- octal(resto, valor);
- System.out.println();
- espera();
- break;
- case 3:
- break;
- default:
- System.out.println("opcion no valida");
- }
- }while(opcion2 != 3);
- }
- public static void binario(Stack <Integer> pila, int valor) {
- int resto;
- while(valor > 0) {
- resto = valor % 2;
- pila.push(resto);
- valor= valor/2;
- }
- while(!pila.empty()) {
- System.out.print(pila.pop());
- }
- }
- public static void octal(Stack <Integer> pila, int valor) {
- int resto;
- while(valor > 0) {
- resto = valor % 8;
- pila.push(resto);
- valor = valor/8;
- }
- while(!pila.empty()){
- System.out.print(pila.pop());
- }
- }
- public static void espera (){ //Este modulo detiene el proceso hasta que el ususario precione "enter"
- Scanner waitForKeypress = new Scanner(System.in);
- System.out.print("Presiona la tecla Enter para continuar....");
- waitForKeypress.nextLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment