Advertisement
Guest User

8labdopzad2

a guest
May 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8.  
  9. import java.math.BigInteger;
  10. public class Main {
  11.     public static void main(String[] args) {
  12.         Pattern l = Pattern.compile("^\\d{1,2}|100$");
  13.         JLabel l1;
  14.         JFrame Window = new JFrame("Вычисление факториала");
  15.         JPanel panel = new JPanel();
  16.         panel.setLayout(new GridLayout(5,1));
  17.         l1 = new JLabel();
  18.         l1.setText("Введите натуральное число");
  19.         JTextField aF = new JTextField();
  20.         JButton ready = new JButton();
  21.         JButton canc = new JButton();
  22.         JLabel Res = new JLabel();
  23.         ready.setText("Решить");
  24.         canc.setText("Отмена");
  25.         ready.addActionListener(e -> {
  26.             String chislo = aF.getText();
  27.             boolean isChislo = l.matcher(chislo).matches();
  28.             long a;
  29.             if(isChislo){
  30.                 BigInteger factorial = BigInteger.valueOf(1);
  31.  
  32.                 a = Long.parseLong(aF.getText());
  33.                 if(a < 0)
  34.                     Res.setText("Число меньше нуля");
  35.                 if (a == 0 || a == 1)
  36.                     Res.setText("Факториал= "+a);
  37.                 for (int i = 2; i <= a; i++) {
  38.                     factorial = factorial.multiply(BigInteger.valueOf(i));
  39.                     if (i >= a)
  40.                         Res.setText("Факториал= " + factorial + chislo);
  41.                 }
  42.             }
  43.             else {
  44.                 Res.setText("Введите число");
  45.             }
  46.  
  47.         });
  48.         canc.addActionListener(e -> {
  49.             canc.setVisible(false);
  50.             System.exit(0);
  51.         });
  52.  
  53.         panel.add(l1);
  54.         panel.add(aF);
  55.         panel.add(ready);
  56.         panel.add(canc);
  57.         panel.add(Res);
  58.         Window.add(panel);
  59.         Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  60.         Window.setSize(300, 250);
  61.         Window.setVisible(true);
  62.  
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement