Java: Convertir de decimal a Romano

decimalRomano.java

package decimalromano;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class decimalRomano extends JFrame{

    private JTextField decimal = new JTextField(6);
    private JTextField romano   = new JTextField(8);

    public static void main(String[] args) {
        decimalRomano ventana = new decimalRomano();
    }

    public decimalRomano() {
        JButton botonConvierte = new JButton("Convierte");

        ActionListener realizaAccion = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int valor = Integer.parseInt(decimal.getText());
                    String roman = Romano.enteroARomano(valor);
                    romano.setText("" + roman);
                } catch (NumberFormatException ex) {
                    romano.setText("Error");
                }
            }};

        botonConvierte.addActionListener(realizaAccion);
        decimal.addActionListener(realizaAccion);

        JPanel contenido = new JPanel();
        contenido.setLayout(new FlowLayout());
        contenido.add(decimal);
        contenido.add(botonConvierte);
        contenido.add(romano);

        this.setContentPane(contenido);
        this.setTitle("Decimal a Romano");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

Romano.java

package decimalromano;

public class Romano {

    final static valorRomano[] TABLAVALORESROMANOS = {
        new valorRomano(1000, "M"),
        new valorRomano( 900, "CM"),
        new valorRomano( 500, "D"),
        new valorRomano( 400, "CD"),
        new valorRomano( 100, "C"),
        new valorRomano(  90, "XC"),
        new valorRomano(  50, "L"),
        new valorRomano(  40, "XL"),
        new valorRomano(  10, "X"),
        new valorRomano(   9, "IX"),
        new valorRomano(   5, "V"),
        new valorRomano(   4, "IV"),
        new valorRomano(   1, "I")
    };

    public static String enteroARomano(int n) {
        if (n >= 4000  || n = equiv.intVal) {
                n -= equiv.intVal;
                resultado.append(equiv.romVal);
            }
        }
        return resultado.toString();
    }

    private static class valorRomano {

        int    intVal;
        String romVal;
        valorRomano(int dec, String rom) {
            this.intVal = dec;
            this.romVal = rom;
        }
    }

}
Advertisement
  1. Dejar un comentario

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.