• Lenguaje

    Java usando Swing

  • Descripción

    Determinar si un número es múltiplo de 2, de 3, o de ninguno de ellos. Considere que existen números que pueden ser múltiplos de más de un número. Por ejemplo: si se ingresa 12 debe mostrarse "El número es múltiplo de 3", "El número es múltiplo de 2".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MultiploDe2O3 extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JTextField field_numero;
    private JButton button;

    public Algoritmo() {
        field_numero = new JTextField(4);
        button = new JButton("Procesar");
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        JPanel panel, subpanel;
        panel = new JPanel(new BorderLayout());
        subpanel = new JPanel(new GridLayout(1, 1));
        subpanel.add(new JLabel("Ingresa el valor de numero:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new JPanel(new GridLayout(1, 1));
        subpanel.add(field_numero);
        panel.add(subpanel);
        pane.add(panel, BorderLayout.NORTH);
        panel = new JPanel(new FlowLayout());
        panel.add(button);
        pane.add(panel);
        button.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        int numero;
        try {
            numero = Integer.parseInt(field_numero.getText());
        } catch (NumberFormatException numberFormatException) {
            return;
        }
        if(numero%2==0)
            javax.swing.JOptionPane.showMessageDialog(this, "El n\u00FAmero es m\u00FAltiplo de 2.");
        if(numero%3==0)
            javax.swing.JOptionPane.showMessageDialog(this, "El n\u00FAmero es m\u00FAltiplo de 3.");
        if(numero%2!=0&&numero%3!=0)
            javax.swing.JOptionPane.showMessageDialog(this, "No es m\u00FAltiplo de ninguno de ellos.");
        pack();
    }

    public static void main(String[] args) {
        new Algoritmo().setVisible(true);
    }

}