Advertisement
nikeza

1.Fill the Matrix

Sep 23rd, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class matrix1_Exercises_1Fill_The_Matrix {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String[] input = scanner.nextLine().split(", ");
  8.  
  9.  
  10.         int row = Integer.parseInt(input[0]);
  11.         int[][] matrix = new int[row][row];
  12.         int count = 0;
  13.         int r = 0;
  14.         int c = 0;
  15.         boolean flag = true;
  16.         if (input[1].equals("A")) {
  17.             while (count++ < row * row) {
  18.  
  19.                 matrix[r][c] = count;
  20.                 if (matrix[r][c] == row * row) {
  21.                     break;
  22.                 }
  23.                 if (r == row - 1) {
  24.                     c++;
  25.                     r = -1;
  26.                 }
  27.                 r++;
  28.  
  29.  
  30.             }
  31.         } else if (input[1].equals("B")) {
  32.             while (count++ < row * row) {
  33.  
  34.                 matrix[r][c] = count;
  35.                 if (matrix[r][c] == row * row) {
  36.                     break;
  37.                 }
  38.                 if (r == row - 1 && flag) {
  39.                     c++;
  40.                     flag = false;
  41.                     r++;
  42.                 }
  43.                 if (r == 0 && !flag) {
  44.                     c++;
  45.                     flag = true;
  46.                     r--;
  47.                 }
  48.                 if (!flag) {
  49.                     r--;
  50.                 } else {
  51.                     r++;
  52.                 }
  53.  
  54.             }
  55.         }
  56.  
  57.         for (int i = 0; i < row; i++) {
  58.             for (int j = 0; j < row; j++) {
  59.                 System.out.print(matrix[i][j] + " ");
  60.             }
  61.             System.out.println();
  62.         }
  63.        
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement