Не удается корректно запустить - Java

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

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

package com.company;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
 
import static java.lang.StrictMath.abs;
 
class MatrixCalculation {
    double[][] GetMinor(double[][] matrix, int row, int column) {
        int minorLength = matrix.length - 1;
        double[][] minor = new double[minorLength][minorLength];
        int dI = 0;
        int dJ = 0;
        for (int i = 0; i <= minorLength; i++) {
            dJ = 0;
            for (int j = 0; j <= minorLength; j++) {
                if (i == row) {
                    dI = 1;
                } else {
                    if (j == column) {
                        dJ = 1;
                    } else {
                        minor[i - dI][j - dJ] = matrix[i][j];
                    }
                }
            }
        }
 
        return minor;
 
    }

        public double CalculateMatrix(double[][] matrix) {
            double calcResult = 0.0;
            if (matrix.length == 2) {
                calcResult = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
            } else {
                int koeff = 1;
                for (int i = 0; i < matrix.length; i++) {
                    if (i % 2 == 1) {
                        koeff = -1;
                    } else {
                        koeff = 1;
                    }
                    ;
                    calcResult += koeff * matrix[0][i] * this.CalculateMatrix(this.GetMinor(matrix, 0, i));
                }
            }
 
            return calcResult;
        }
 
    }

public class Main extends JFrame {
public Main(String name)  { super(name); }
 
    private static void createAndShowGUI() {
        Main frame = new Main("ContainerDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Container contentPane = frame.getContentPane();
        JPanel panel1 = new JPanel();
        JButton b = new JButton("Press");
        JButton c = new JButton("Fill");
        JButton d = new JButton("Test");
        final int[] n = new int[1];
        double a[][] = new double[100][100];
        panel1.add(b);
        panel1.add(c);
        panel1.add(d);
        JTextField[][] visMatrix = new JTextField[100][100];
        Insets ins = frame.getInsets();
        frame.setLayout(null);
        //panel1.setLayout(null);

        contentPane.add(panel1);
        JPanel panel2 = new JPanel();
        JTextField tf = new JTextField("n");
        panel2.add(tf);
        JLabel j1 = new JLabel("Matrix");

        panel2.add(j2);
        panel2.add(j1);
        final int[] R = new int[1];

        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                R[0] = Integer.parseInt(tf.getText());

                final int w = 30;
                final int h = 20;
                for (int i = 0; i < R[0]; i++)
                    for (int j = 0; j < R[0]; ++j) {
                        visMatrix[i][j] = new JTextField();
                        panel1.add(visMatrix[i][j]);
                        visMatrix[i][j].setBounds(ins.left + w * i, ins.top + h * j, w, h);
                        contentPane.add(new JTextField("TextField" + i + " " + j));
                        contentPane.add(visMatrix[i][j]);
                                        }
                                }
        });
        c.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int R = Integer.parseInt(tf.getText());
                Random r = new Random();
                Integer x;
                for (int i = 0; i < R; i++) {
                    for (int j = 0; j < R; ++j) {
                        x = r.nextInt(100) - 50;
                        visMatrix[i][j].setText(x.toString());
                      a[i][j]=Integer.parseInt(visMatrix[i][j].getText());
                    }
                }
            }
        });
 
//MatrixCalculation mc = new MatrixCalculation();
      //  double Result = mc.CalculateMatrix(a);
 
        d.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextField q=new JTextField();
                panel1.add(q);
                q.setBounds(100,100,50,50);
                q.setText(String.valueOf(/* Result */);
 
            }
        });

        contentPane.add(panel2);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                                       @Override
                                       public void run() {
                                           createAndShowGUI();
                                       }
                                   }

        );
 
    }
 
}
Про код. 11-57 класс считающий определитель матрицы nxn 61-153 задание визуальной матрицы 160- main. Проблема в том, что в строчках 132-133 пытаюсь посчитать определитель с помощью класса записанного на 11-57 строчках. Если без них, то все хорошо работает, т.е выдает матрицу при нажатии на кнопки. А с ними даже визуально не выдает никакого результата, то ли программа зацикливается, то ли что, понять не могу. Подскажите, куда в сунуть данный класс или может просто функцией оформить. P.s задание построить матрицу nxn и найти её определитель.

Решение задачи: «Не удается корректно запустить»

textual
Листинг программы
 static double CalculateMatrix(double[][] matrix, int t){
        int determinant = 1;
        int exchange = 0;
        for(int l1=0; l1<t-1; ++l1){
            int maxN = l1;
            double maxValue = Math.abs(matrix[l1][l1]);
            for (int l2 = l1+1; l2<t; l2++){
                double value = Math.abs(matrix[l2][l1]);
                if (value > maxValue) {maxN=l2; maxValue=value;}
            }
            if (maxN > l1)
            {
 
                double[] temp = matrix[l1]; matrix[l1]=matrix[maxN]; matrix[maxN]=temp;
                ++exchange;
 
            } else {
                if(maxValue == 0) return maxValue;
            }
            double value = matrix[l1][l1];
            determinant *=value;
 
            for (int l2=l1+1;l2<t; ++l2)
            {
                double k = matrix[l2][l1]/value;
                matrix[l2][l1] = 0;
                for (int c=l1+1; c<t; ++c) matrix[l2][c] -= matrix [l1][c]*k;
 
            }
 
        }
determinant *= matrix[t-1][t-1];
        if (exchange%2!=0) return -determinant; else return determinant;
 
 
    }

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


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

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

9   голосов , оценка 4 из 5