desislava_topuzakova

02. Matrix of Palindromes

Jan 15th, 2022
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package MultidimensionalArrays_02;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class MatrixOfPalindromes_02 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int rows = scanner.nextInt();
  9.         int cols = scanner.nextInt();
  10.  
  11.         String[][] matrix = new String[rows][cols];
  12.  
  13.         char startLetter = 'a';
  14.         for (int row = 0; row < rows ; row++) {
  15.             for (int col = 0; col < cols ; col++) {
  16.                 //(a -> b -> c)(startLetter + 1)()
  17.                 String palindrome = "" + startLetter + (char)(startLetter + col) + startLetter;
  18.                 matrix[row][col] = palindrome;
  19.             }
  20.             startLetter = (char)(startLetter + 1);
  21.         }
  22.  
  23.         printMatrix(matrix, rows, cols);
  24.     }
  25.  
  26.     private static void printMatrix(String[][] matrix, int rows, int cols) {
  27.         for (int row = 0; row < rows; row++) {
  28.             for (int col = 0; col < cols; col++) {
  29.                 System.out.print(matrix[row][col] + " ");
  30.             }
  31.             System.out.println();
  32.         }
  33.     }
  34. }
RAW Paste Data