blikk info infothek forum galerie sitemap

Multiplikation von Matrizen

zur Aufgabenstellung

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

 

public class Matrizen {

 

            public static void main(String[] args) {

 

                        BufferedReader eingabe = new BufferedReader(new InputStreamReader(

                                               System.in));

 

                        try {

                                   System.out.println("Wie viele Zeilen/Spalten möchten Sie?");

                                   int zeilen = Integer.parseInt(eingabe.readLine());

 

                                   int Matrix1[][] = new int[zeilen][zeilen];

                                   int Matrix2[][] = new int[zeilen][zeilen];

                                   System.out.println("nGeben Sie bitte die erste Matrix ein!");

                                   EingabeMatrix(Matrix1, zeilen);

                                   System.out.println("nGeben Sie bitte die zweite Matrix ein!");

                                   EingabeMatrix(Matrix2, zeilen);

                                   System.out.println("Das ist die erste Matrix");

                                   AusgabeMatrix(Matrix1, zeilen);

                                   System.out.println("Das ist die zweite Matrix");

                                   AusgabeMatrix(Matrix2, zeilen);

                                   MultiplikationMatrix(Matrix1, Matrix2, zeilen);

                        } catch (IOException z) {

                                   System.out.println("Problem beim Einlesen");

                        }

            }

 

            static void EingabeMatrix(int[][] Matrizen, int zeilen) {

                        BufferedReader eingabe = new BufferedReader(new InputStreamReader(

                                               System.in));

                        for (int i = 0; i < zeilen; i++) {

                                   for (int j = 0; j < zeilen; j++) {

                                               try {

                                                           System.out.println("Geben Sie die Zahl der " + (i + 1)

                                                                                  + ". Zeile und der " + (j + 1) + ". Spalte ein: ");

                                                           Matrizen[i][j] = Integer.parseInt(eingabe.readLine());

                                               } catch (IOException z) {

                                                           System.out.println("Problem beim Einlesen");

                                               }

                                   }

                        }

 

            }

 

            static void AusgabeMatrix(int[][] Matrix1, int zeilen) {

                        for (int i = 0; i < zeilen; i++) {

                                   for (int j = 0; j < zeilen; j++) {

                                               System.out.print(Matrix1[i][j]);

                                               System.out.print("t");

                                               if (zeilen == (j + 1)) {

                                                           System.out.println();

                                                           System.out.println();

 

                                               }

                                   }

                        }

            }

 

            static void MultiplikationMatrix(int[][] Matrix1, int[][] Matrix2,

                                   int zeilen) {

                        int[][] Addm = new int[zeilen][zeilen];

                        for (int i = 0; i < zeilen; i++) {

                                   for (int j = 0; j < zeilen; j++) {

                                               for (int aktuellespalte = 0; aktuellespalte < zeilen; aktuellespalte++) {

                                                           Addm[i][j] = Addm[i][j] + Matrix1[i][aktuellespalte]

                                                                                  * Matrix2[aktuellespalte][j];

                                               }

                                   }

                        }

                        System.out.println();

                        System.out.println();

                        System.out.println("Ausgabe Matrix: ");

                        AusgabeMatrix(Addm, zeilen);

            }

}

 

nach oben