-
Lenguaje
Java usando Swing
-
Descripción
Imprima la suma de los dígitos de un número entre 1 y 99. Por ejemplo, si el número es 24 debe imprimir el valor de la suma de los dígitos (2+4=6).
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
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SumaDeLosDigitosDeUnNumero extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel label_suma;
private JButton button;
public Algoritmo() {
label_suma = new JLabel("", JLabel.RIGHT);
button = new JButton("Procesar");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(new JLabel("Valor de suma:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(label_suma);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int suma;
suma=(i-i%10)/10+i%10;
label_suma.setText(String.valueOf(suma));
pack();
}
public static void main(String[] args) {
new Algoritmo().setVisible(true);
}
}
import java.awt.event.*;
import javax.swing.*;
public class SumaDeLosDigitosDeUnNumero extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel label_suma;
private JButton button;
public Algoritmo() {
label_suma = new JLabel("", JLabel.RIGHT);
button = new JButton("Procesar");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
JPanel panel, subpanel;
panel = new JPanel(new FlowLayout());
panel.add(button);
pane.add(panel);
panel = new JPanel(new BorderLayout());
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(new JLabel("Valor de suma:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new JPanel(new GridLayout(1, 1));
subpanel.add(label_suma);
panel.add(subpanel);
pane.add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
int suma;
suma=(i-i%10)/10+i%10;
label_suma.setText(String.valueOf(suma));
pack();
}
public static void main(String[] args) {
new Algoritmo().setVisible(true);
}
}