Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.math.BigInteger;
- public class Main {
- public static void main(String[] args) {
- Pattern l = Pattern.compile("^\\d{1,2}|100$");
- JLabel l1;
- JFrame Window = new JFrame("Вычисление факториала");
- JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(5,1));
- l1 = new JLabel();
- l1.setText("Введите натуральное число");
- JTextField aF = new JTextField();
- JButton ready = new JButton();
- JButton canc = new JButton();
- JLabel Res = new JLabel();
- ready.setText("Решить");
- canc.setText("Отмена");
- ready.addActionListener(e -> {
- String chislo = aF.getText();
- boolean isChislo = l.matcher(chislo).matches();
- long a;
- if(isChislo){
- BigInteger factorial = BigInteger.valueOf(1);
- a = Long.parseLong(aF.getText());
- if(a < 0)
- Res.setText("Число меньше нуля");
- if (a == 0 || a == 1)
- Res.setText("Факториал= "+a);
- for (int i = 2; i <= a; i++) {
- factorial = factorial.multiply(BigInteger.valueOf(i));
- if (i >= a)
- Res.setText("Факториал= " + factorial + chislo);
- }
- }
- else {
- Res.setText("Введите число");
- }
- });
- canc.addActionListener(e -> {
- canc.setVisible(false);
- System.exit(0);
- });
- panel.add(l1);
- panel.add(aF);
- panel.add(ready);
- panel.add(canc);
- panel.add(Res);
- Window.add(panel);
- Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Window.setSize(300, 250);
- Window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement