The program in java does not print the result

0

I try to make a program that converts a decimal number to binary, octal and hexadecimal I made a menu and everything, the binary conversion works but when entering the number in the other conversions, it just does not print anything.  here the program.

The object

package com.modelo;
public class Numero {
private String num;

public Numero() {
    num = null;
}
public Numero (String num) {
    this.num = num;
}

public String getNum () {
    return num;
}
public void setNum (String num) {
    this.num = num;
}

}    

The interfaces

package com.control;
import com.modelo.Numero;

public interface conversiones {
public int decimal(Numero n); 
public int octal(Numero n);
public int hexa(Numero n);

}

The class that is supposed to print the result

package com.tester;
import com.control.Metodos;
import com.control.control; 
import com.modelo.Numero;
import java.util.Scanner;
public class Tester {

public static void main(String args[]) {
    Scanner entrada=new Scanner(System.in);
    control ctr=new control();
    Numero n=new Numero();
    int op;
    do {
        System.out.println("¿Que conversion desea realizar?:\n 1- Decimal a Binario \n 2- decimal a octal \n 3- decimal a Hexadecimal");
        op = entrada.nextInt();
        switch (op) {
        case 1:
            System.out.println ("Ingrese el numero: ");
            n.setNum(entrada.nextInt());
            System.out.println (ctr.decimal(n));
            break;
        case 2:
            System.out.println ("Ingrese el numero: ");
            n.setNum(entrada.nextInt());
            System.out.println (ctr.octal(n));
            break;
        case 3:
            System.out.println ("Ingrese el numero: ");
            n.setNum(entrada.nextInt());
            System.out.println (ctr.hexa(n));
            break;
        case 4:
            System.out.println("Finalizado");
            break;
        default:
            System.out.println("Opcion Ivalida");
            break;  
        }
        } while (op != 4);
    }
}

The class that does the operations

package com.control;

import com.modelo.Numero;

public class control implements conversiones {

public String imprime(Numero n) {
    StringBuilder sb = new StringBuilder("");
    sb.append("Numero: ").append(n.getNum());
    sb.append("Binario: ").append(decimal(n));
    sb.append("Octal: ").append(octal(n));
    sb.append("Hexadecimal: ").append(hexa(n));
    return sb.toString();
}

@Override
public String decimal(Numero n) {
    String bin = new String("");
    while (n.getNum() > 0 ) {
        bin = n.getNum() % 2 + bin;
        n.setNum(n.getNum() / 2);

                }
    return bin;
} 

@Override
public String octal(Numero n) {

    String oct = new String("");
    while (n.getNum() >= 0) {
        oct = n.getNum() % 8 + oct;
        n.setNum(n.getNum() / 8);
    }
    return oct;
}

@Override
public String hexa(Numero n) {

    String hex = new String("");
    int resi = 0;
    while (n.getNum() >= 0) {
        resi = n.getNum() % 16;
        if (resi >= 0 && resi <= 9) {
            hex = resi + hex;
        } else {
            switch (resi) {
            case 10:
                hex = "A" + hex;
                break;
            case 11:
                hex = "B" + hex;
                break;
            case 12:
                hex = "C" + hex;
                break;
            case 13:
                hex = "D" + hex;
                break;
            case 14:
                hex = "E" + hex;
                break;
            case 15:
                hex = "F" + hex;
                break;
            }
            n.setNum(n.getNum() / 16);
        }
    }
    return hex;
}
}

    
asked by Star Hikari 01.11.2018 в 20:15
source

1 answer

-1

What happens that in your cycles you have a greater or equal to 0, then there comes a time when all the results give zero but the cycle keeps running, since it continues to accept the condition and therefore cycles.

You must change your while this way:

public String octal(Numero n) {

String oct = new String("");

while (n.getNum() > 0) {
    oct = n.getNum() % 8 + oct;
    n.setNum(n.getNum() / 8);
}
return oct;
}

Similarly for the Hexadecimal.

Try it.

    
answered by 01.11.2018 / 21:00
source