Advertisement
nikitaxe132

Untitled

Oct 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4.     static int enter(int max, int min) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int temp = 0;
  7.         boolean iscorrect = true;
  8.         do {
  9.             try {
  10.                 temp = scanner.nextInt();
  11.                 if ((temp > min -1) && (temp < max + 1)) {
  12.                     iscorrect = false;
  13.                 }
  14.                 else {
  15.                     System.out.println("This is a mistake. Please enter again!");
  16.                 }
  17.             }catch (Exception e) {
  18.                 scanner.nextLine();
  19.                 System.out.println("This is a mistake. Please enter again!");
  20.                 iscorrect = true;
  21.             }
  22.         }
  23.         while (iscorrect);
  24.         return temp;
  25.     }
  26.     public static void main(String[] args) {
  27.         int[][] matrix1 = new int[10][10];
  28.         int[][] matrix2 = new int[10][10];
  29.         int flag;
  30.         int n;
  31.         System.out.print("Enter the number of rows and columns in the matrix (N > 1 and N < 11) = ");
  32.         n = enter(10,2);
  33.         System.out.print("Enter the elements of first matrix a");
  34.         for (int i = 0; i < n; i++) {
  35.             for (int j = 0; j < n; j++ ) {
  36.                 System.out.print("Enter a[" + (i + 1) + "," + (j + 1) + "] = ");
  37.                 matrix1[i][j] = enter(200000,-200000);
  38.             }
  39.         }
  40.         System.out.print("Enter the elements of first matrix b");
  41.         for (int i = 0; i < n; i++) {
  42.             for (int j = 0; j < n; j++ ) {
  43.                 System.out.print("Enter b[" + (i + 1) + "," + (j + 1) + "] = ");
  44.                 matrix2[i][j] = enter(200000,-200000);
  45.             }
  46.         }
  47.         System.out.print("Enter 1 for sum or 2 for subtract matrices ");
  48.         flag = enter(2,1);
  49.         if (flag == 1) {
  50.             for (int i = 0; i < n; i++) {
  51.                 for (int j = 0; j < n; j++ ) {
  52.                     matrix1[i][j] = matrix1[i][j] + matrix2[i][j];
  53.                 }
  54.             }
  55.         }
  56.         else {
  57.             for (int i = 0; i < n; i++) {
  58.                 for (int j = 0; j < n; j++ ) {
  59.                     matrix1[i][j] = matrix1[i][j] - matrix2[i][j];
  60.                 }
  61.             }
  62.         }
  63.         System.out.print("New matrix");
  64.         for (int i = 0; i < n; i++) {
  65.             System.out.println();
  66.             for (int j = 0; j < n; j++ ) {
  67.                 System.out.print(matrix1[i][j] +  "  ");
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement