Как сделать так, что бы можно было вводить данные в форму и давал вывод в той же форме? - Java
Формулировка задачи:
вот сам код!
САМО УСЛОВИЕ ВОТ В массиве b = b1,...,bn найти номера трёх последовательных элементов, значения которых удовлетворяют неравенству bi + bi+1 + bi+2 > 0
Листинг программы
- import javax.swing.*;
- import java.awt.*;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
- class Main {
- public static void main(String[] args) {
- new Frame();
- }
- static class Frame extends JFrame {
- JButton b1, b2, b3;
- JTextArea consol;
- String text;
- Frame(){
- b1 = new JButton("1-Способ±");
- b1.addActionListener(e -> sposob1());
- add(b1);
- b2 = new JButton("2-Способ±");
- b2.addActionListener(e -> sposob2());
- add(b2);
- b3 = new JButton("3-Способ±");
- b3.addActionListener(e -> sposob3());
- add(b3);
- consol = new JTextArea(30,40);
- add(consol);
- JScrollPane JS = new JScrollPane(consol);
- JS.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- add(JS);
- setTitle("Приложение");
- setSize(400,300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setResizable(true);
- setLocationRelativeTo(null);
- setLayout(new FlowLayout());
- setVisible(true);
- }
- public void sposob1(){ // ИМЕННО В ЭТОЙ КНОПКЕ МОЖНО БЫЛО ВВОДИТЬ ДАННЫЕ В ФОРМУ И ВЫВОДИЛ ДАННЫЕ В ЭТОЙ ФОРМЕ
- text = "";
- Scanner scn = new Scanner(System.in);
- System.out.println("Введите размер массива: ");
- int size = scn.nextInt();
- int[] a = new int[size];
- System.out.println("Введите значения элементов массива: ");
- for (int i = 0; i < size; i++)
- a[i] = scn.nextInt();
- for (int i = 0; i < size; i++)
- if (i < size - 2)
- if (a[i] + a[i + 1] + a[i + 2] > 0)
- if (i == size - 3)
- text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2];
- else
- text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2] + "\n\n";
- consol.setText(text);
- }
- public void sposob2(){
- text = "";
- int size = new Random().nextInt(9)+1;
- int[] a = new int[size];
- for (int i = 0; i < size; i++)
- a[i] = new Random().nextInt(10);
- for (int i = 0; i < size; i++)
- if (i < size - 2)
- if (a[i] + a[i + 1] + a[i + 2] > 0)
- if (i == size - 3)
- text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2];
- else
- text += "a[i] = " + a[i] + "\na[i+1] = " + a[i + 1] + "\na[i+2] = " + a[i + 2] + "\n\n";
- consol.setText(text);
- }
- public void sposob3(){
- text = "";
- ArrayList<Integer> a = new ArrayList<>();
- a.add(1);
- a.add(2);
- a.add(-1);
- a.add(-3);
- a.add(4);
- a.add(7);
- a.add(-7);
- a.add(8);
- a.add(-1);
- a.add(0);
- for (int i = 0; i < 8; i++)
- if (a.get(i) + a.get(i + 1) + a.get(i + 2) > 0)
- if (i == 7)
- text += "\na[i] = " + a.get(i) + "\na[i+1] = " + a.get(i + 1) + "\na[i+2] = " + a.get(i + 2);
- else
- text += "\na[i] = " + a.get(i) + "\na[i+1] = " + a.get(i + 1) + "\na[i+2] = " + a.get(i + 2) + "\n\n";
- consol.setText(text);
- }
- }
- }
Решение задачи: «Как сделать так, что бы можно было вводить данные в форму и давал вывод в той же форме?»
textual
Листинг программы
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class MassiveRun {
- public static int[] mass = {1, 2, -1, -3, 4, 7, -7, 8, -1, 0};
- public static void main(String[] args) {
- MainFrame frame = new MainFrame();
- JButton button1 = new JButton("Способ 1");
- button1.setBounds(10, 10, 160, 30);
- MainFrame.panel.add(button1);
- JButton button2 = new JButton("Способ 2");
- button2.setBounds(180, 10, 160, 30);
- MainFrame.panel.add(button2);
- JButton button3 = new JButton("Способ 3");
- button3.setBounds(350, 10, 160, 30);
- MainFrame.panel.add(button3);
- MainFrame.panel.updateUI();
- button1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- ButtonEvents click = new ButtonEvents();
- click.clickButton1();
- }
- });
- button2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- ButtonEvents click2 = new ButtonEvents();
- click2.clickButton2();
- }
- });
- button3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- ButtonEvents click3 = new ButtonEvents();
- click3.clickButton3();
- }
- });
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д