• Lenguaje

    Java usando Swing

  • Descripción

    Determine en un conjunto de 100 números naturales cuantos son menores de 15, mayores de 50 y cuantos están comprendidos entre 45 y 55.

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 ConjuntoDe100Numeros extends JFrame implements ActionListener {

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

    public Algoritmo() {
        field_numero_natural = 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 natural:"));
        panel.add(subpanel, BorderLayout.WEST);
        subpanel = new JPanel(new GridLayout(1, 1));
        subpanel.add(field_numero_natural);
        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 entre_45_y_55, mayores_de_50, menores_de_15, numero_natural;
        try {
            numero_natural = Integer.parseInt(field_numero_natural.getText());
        } catch (NumberFormatException numberFormatException) {
            return;
        }
        if(numero_natural<15)
            menores_de_15=menores_de_15+1;
        if(numero_natural>50)
            mayores_de_50=mayores_de_50+1;
        if(numero_natural>=45&&numero_natural<=55)
            entre_45_y_55=entre_45_y_55+1;
        pack();
    }

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

}