Advertisement
remote87

TheMatrix

Dec 1st, 2020 (edited)
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. // винаги използвайте клас Main, инак ще получите грешка при компилиране!
  4. public class Main {
  5.   public static Scanner scan = new Scanner(System.in);
  6.     static int rows = 0;
  7.     static int cols = 0;
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         int[][] matrixOne = matrix(1);
  12.         int[][] matrixTwo = matrix(2);
  13.  
  14.         printMatrixWithSymbol(matrixOne, matrixTwo);
  15.  
  16.         sumMatrix(matrixOne, matrixTwo);
  17.  
  18.         directSumMatrix(matrixOne, matrixTwo);
  19.     }
  20.  
  21.     public static int[][] matrix(int num){
  22.  
  23.         if(num == 1){
  24.  
  25.             do {
  26.                 System.out.print("Брой редове: ");
  27.                 rows = Integer.parseInt(scan.nextLine());
  28.             } while (rows >= 10 || rows < 0);
  29.  
  30.             do {
  31.                 System.out.print("Брой колони: ");
  32.                 cols = Integer.parseInt(scan.nextLine());
  33.             } while (cols >= 10 || cols < 0);
  34.  
  35.             int[][] matrix = new int[rows][cols];
  36.  
  37.             System.out.println("Въведете матрица 1:");
  38.             for(int i = 0; i < rows; i++){
  39.                 for(int j = 0; j < cols; j++){
  40.                     System.out.print("Ред " + i + ", Колона " + j + " = ");
  41.                     matrix[i][j] = Integer.parseInt(scan.nextLine());
  42.                 }
  43.             }
  44.             return matrix;
  45.         }else {
  46.             int[][] matrix = new int[rows][cols];
  47.  
  48.             System.out.println("Въведете матрица 2:");
  49.             for (int i = 0; i < rows; i++) {
  50.                 for (int j = 0; j < cols; j++) {
  51.                     System.out.print("Ред " + i + ", Колона " + j + " = ");
  52.                     matrix[i][j] = Integer.parseInt(scan.nextLine());
  53.                 }
  54.             }
  55.             return matrix;
  56.         }
  57.     }
  58.  
  59.     public static void justPrintTwoMatrix(int[][] matrixOne, int[][] matrixTwo){
  60.         for(int i = 0; i < matrixOne.length; i++) {
  61.             for (int j = 0; j < matrixOne[i].length; j++) {
  62.                 System.out.printf("%-4d", matrixOne[i][j]);
  63.             }
  64.             for(int j = 0; j < matrixOne[i].length; j++){
  65.                 System.out.printf("%-4d", matrixTwo[i][j]);
  66.             }
  67.             System.out.println();
  68.         }
  69.     }
  70.  
  71.     public static void printMatrixWithSymbol(int[][] matrixOne, int[][] matrixTwo){
  72.         for(int i = 0; i < matrixOne.length; i++){
  73.             for(int j = 0; j < matrixOne[i].length; j++){
  74.                 System.out.printf("%-4d", matrixOne[i][j]);
  75.             }
  76.             System.out.print(" | ");
  77.             for(int j = 0; j < matrixOne[i].length; j++){
  78.                 System.out.printf("%-4d", matrixTwo[i][j]);
  79.             }
  80.             System.out.println();
  81.         }
  82.     }
  83.  
  84.     public static void sumMatrix(int[][] matrixOne, int[][] matrixTwo){
  85.         System.out.println("Сумата на матриците е:");
  86.         for(int i = 0; i < matrixOne.length; i++){
  87.             for(int j = 0; j < matrixOne[i].length; j++){
  88.                 System.out.printf("%-4d", matrixOne[i][j] +matrixTwo[i][j]);
  89.             }
  90.             System.out.println();
  91.         }
  92.     }
  93.  
  94.     public static void directSumMatrix(int[][] matrixOne, int[][] matrixTwo){
  95.         int[][] directSumMatrix = new int[rows * cols][rows * cols];
  96.  
  97.         System.out.println("Директната сумата на матриците е:");
  98.  
  99.         int[][] nullMatrix = new int[rows][cols];
  100.  
  101.         justPrintTwoMatrix(matrixOne, nullMatrix);
  102.         justPrintTwoMatrix(nullMatrix, matrixTwo);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement