-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathFactorial.java
59 lines (47 loc) · 1.58 KB
/
Factorial.java
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
56
57
58
59
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
public class Factorial extends JFrame {
Factorial(int n) {
this.setSize(270, 120);
this.setTitle("Factorial Solver!");
JPanel panel = new JPanel();
this.add(panel);
JTextField nField = new JTextField(11);
JTextField resultField = new JTextField(18);
JLabel nLabel = new JLabel();
nLabel.setText("n = ");
JLabel resultLabel = new JLabel();
resultLabel.setText("n! = ");
JButton calcBtn = new JButton();
calcBtn.setText("Calculate");
panel.add(nLabel);
panel.add(nField);
panel.add(calcBtn);
panel.add(resultLabel);
panel.add(resultField);
calcBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int n = Integer.parseInt(nField.getText());
int fact = 1;
if (n == 1)
fact = 1;
else
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
String result = Integer.toString(fact);
resultField.setText(result);
}
});
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new Factorial(20);
}
}