Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

행렬의 경계 요소를 인쇄하는 Java 프로그램

<시간/>

이 기사에서는 행렬의 경계 요소를 인쇄하는 방법을 이해할 것입니다. 행렬은 행과 열의 요소를 나타냅니다. 경계 요소는 네 방향 모두에서 요소로 둘러싸여 있지 않은 요소입니다. 예를 들어 첫 번째 행, 첫 번째 열, 마지막 행 및 마지막 열의 요소입니다.

아래는 동일한 데모입니다 -

입력이 다음과 같다고 가정 -

The input matrix:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5

원하는 출력은 -

The border elements of the matrix is:
9 8 9 8
8     7
7     6
6 5 6 5

알고리즘

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix, an object of the class BoundaryElements namely border_values.
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops and check if the element is a boundary element using Boolean OR condition.
Step 5 - Display the boundary elements.
Step 5 - Stop

예시 1

여기에서는 프롬프트에 따라 사용자가 입력하고 있습니다.

public class BoundaryElements {
   public static void main(String[] args) {
      int input_matrix[][] = new int[][] {
         { 9, 8, 9, 8 },
         { 8, 7, 8, 7 },
         { 7, 6, 7, 6 },
         { 6, 5, 6, 5 }
      };
      System.out.println("The matrix is defined as: ");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            System.out.print(input_matrix[x][y] + " ");
         }
         System.out.println();
      }
      BoundaryElements border_values = new BoundaryElements();
      System.out.println("The border elements of the matrix is:");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            if (x == 0 || y == 0 || x == input_matrix.length - 1
               || y == input_matrix[x].length - 1) {
               System.out.print(input_matrix[x][y] + " ");
            } else {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
}

출력

The matrix is defined as:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5
The border elements of the matrix is:
9 8 9 8
8 7
7 6
6 5 6 5

예시 2

여기서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.

public class BoundryElements {
   public void Boundary_Elements(int input_matrix[][]) {
      System.out.println("The matrix is defined as: ");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            System.out.print(input_matrix[x][y] + " ");
         }
         System.out.println();
      }
      System.out.println("The border elements of the matrix is:");
      for (int x = 0; x < input_matrix.length; x++) {
         for (int y = 0; y < input_matrix[x].length; y++) {
            if (x == 0 || y == 0 || x == input_matrix.length - 1
               || y == input_matrix[x].length - 1) {
               System.out.print(input_matrix[x][y] + " ");
            } else {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
   }
   public static void main(String[] args) {
      int input_matrix[][] = new int[][] {
         { 9, 8, 9, 8 },
         { 8, 7, 8, 7 },
         { 7, 6, 7, 6 },
         { 6, 5, 6, 5 }
      };
      BoundryElements border_values = new BoundryElements();
      border_values.Boundary_Elements(input_matrix);
   }
}

출력

The matrix is defined as:
9 8 9 8
8 7 8 7
7 6 7 6
6 5 6 5
The border elements of the matrix is:
9 8 9 8
8 7
7 6
6 5 6 5