Как сделать так, что бы можно было вводить данные в форму и давал вывод в той же форме? - Java

Узнай цену своей работы

Формулировка задачи:

вот сам код!
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. class Main {
  7. public static void main(String[] args) {
  8. new Frame();
  9. }
  10. static class Frame extends JFrame {
  11. JButton b1, b2, b3;
  12. JTextArea consol;
  13.  
  14. String text;
  15. Frame(){
  16. b1 = new JButton("1-Способ±");
  17. b1.addActionListener(e -> sposob1());
  18. add(b1);
  19. b2 = new JButton("2-Способ±");
  20. b2.addActionListener(e -> sposob2());
  21. add(b2);
  22. b3 = new JButton("3-Способ±");
  23. b3.addActionListener(e -> sposob3());
  24. add(b3);
  25. consol = new JTextArea(30,40);
  26. add(consol);
  27. JScrollPane JS = new JScrollPane(consol);
  28. JS.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  29. add(JS);
  30. setTitle("Приложение");
  31. setSize(400,300);
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. setResizable(true);
  34. setLocationRelativeTo(null);
  35. setLayout(new FlowLayout());
  36. setVisible(true);
  37. }
  38. public void sposob1(){ // ИМЕННО В ЭТОЙ КНОПКЕ МОЖНО БЫЛО ВВОДИТЬ ДАННЫЕ В ФОРМУ И ВЫВОДИЛ ДАННЫЕ В ЭТОЙ ФОРМЕ
  39. text = "";
  40. Scanner scn = new Scanner(System.in);
  41. System.out.println("Введите размер массива: ");
  42. int size = scn.nextInt();
  43. int[] a = new int[size];
  44. System.out.println("Введите значения элементов массива: ");
  45. for (int i = 0; i < size; i++)
  46. a[i] = scn.nextInt();
  47. for (int i = 0; i < size; i++)
  48. if (i < size - 2)
  49. if (a[i] + a[i + 1] + a[i + 2] > 0)
  50. if (i == size - 3)
  51. text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2];
  52. else
  53. text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2] + "\n\n";
  54. consol.setText(text);
  55. }
  56. public void sposob2(){
  57. text = "";
  58. int size = new Random().nextInt(9)+1;
  59. int[] a = new int[size];
  60. for (int i = 0; i < size; i++)
  61. a[i] = new Random().nextInt(10);
  62. for (int i = 0; i < size; i++)
  63. if (i < size - 2)
  64. if (a[i] + a[i + 1] + a[i + 2] > 0)
  65. if (i == size - 3)
  66. text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2];
  67. else
  68. text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2] + "\n\n";
  69. consol.setText(text);
  70. }
  71. public void sposob3(){
  72. text = "";
  73. ArrayList<Integer> a = new ArrayList<>();
  74. a.add(1);
  75. a.add(2);
  76. a.add(-1);
  77. a.add(-3);
  78. a.add(4);
  79. a.add(7);
  80. a.add(-7);
  81. a.add(8);
  82. a.add(-1);
  83. a.add(0);
  84. for (int i = 0; i < 8; i++)
  85. if (a.get(i) + a.get(i + 1) + a.get(i + 2) > 0)
  86. if (i == 7)
  87. text += "\na[i] = " + a.get(i) + "\na[i+1] = " + a.get(i + 1) + "\na[i+2] = " + a.get(i + 2);
  88. else
  89. text += "\na[i] = " + a.get(i) + "\na[i+1] = " + a.get(i + 1) + "\na[i+2] = " + a.get(i + 2) + "\n\n";
  90. consol.setText(text);
  91. }
  92. }
  93. }
САМО УСЛОВИЕ ВОТ В массиве b = b1,...,bn найти номера трёх последовательных элементов, значения которых удовлетворяют неравенству bi + bi+1 + bi+2 > 0

Решение задачи: «Как сделать так, что бы можно было вводить данные в форму и давал вывод в той же форме?»

textual
Листинг программы
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class MassiveRun {
  6.     public static int[] mass = {1, 2, -1, -3, 4, 7, -7, 8, -1, 0};
  7.  
  8.     public static void main(String[] args) {
  9.         MainFrame frame = new MainFrame();
  10.  
  11.         JButton button1 = new JButton("Способ 1");
  12.         button1.setBounds(10, 10, 160, 30);
  13.         MainFrame.panel.add(button1);
  14.  
  15.         JButton button2 = new JButton("Способ 2");
  16.         button2.setBounds(180, 10, 160, 30);
  17.         MainFrame.panel.add(button2);
  18.  
  19.         JButton button3 = new JButton("Способ 3");
  20.         button3.setBounds(350, 10, 160, 30);
  21.         MainFrame.panel.add(button3);
  22.  
  23.         MainFrame.panel.updateUI();
  24.  
  25.         button1.addActionListener(new ActionListener() {
  26.             @Override
  27.             public void actionPerformed(ActionEvent e) {
  28.                 ButtonEvents click = new ButtonEvents();
  29.                 click.clickButton1();
  30.             }
  31.         });
  32.  
  33.         button2.addActionListener(new ActionListener() {
  34.             @Override
  35.             public void actionPerformed(ActionEvent e) {
  36.                 ButtonEvents click2 = new ButtonEvents();
  37.                 click2.clickButton2();
  38.             }
  39.         });
  40.  
  41.         button3.addActionListener(new ActionListener() {
  42.             @Override
  43.             public void actionPerformed(ActionEvent e) {
  44.                 ButtonEvents click3 = new ButtonEvents();
  45.                 click3.clickButton3();
  46.             }
  47.         });
  48.     }
  49. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы