desislava_topuzakova

Untitled

Jan 14th, 2022
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package MultidimensionalArrays_02;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ShufflingMatrix_05 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String dimensions = scanner.nextLine(); //"3 4" -> split(" ") ->["3", "4"]
  9. int rows = Integer.parseInt(dimensions.split(" ")[0]);
  10. int cols = Integer.parseInt(dimensions.split(" ")[1]);
  11.  
  12. String[][] matrix = new String[rows][cols];
  13. fillMatrix(matrix, scanner);
  14.  
  15. String command = scanner.nextLine();
  16. while (!command.equals("END")) {
  17. //валидираме командата
  18. //невалидна
  19. if (!validateCommand(command, rows, cols)) {
  20. System.out.println("Invalid input!");
  21. }
  22. //валидна
  23. else {
  24. //"swap 0 0 1 1"
  25. String [] commandParts = command.split("\\s+");
  26. int row1 = Integer.parseInt(commandParts[1]);
  27. int col1 = Integer.parseInt(commandParts[2]);
  28. int row2 = Integer.parseInt(commandParts[3]);
  29. int col2 = Integer.parseInt(commandParts[4]);
  30.  
  31. String firstElement = matrix[row1][col1];
  32. String secondElement = matrix[row2][col2];
  33.  
  34. matrix[row1][col1] = secondElement;
  35. matrix[row2][col2] = firstElement;
  36.  
  37. printMatrix(matrix, rows, cols);
  38. }
  39.  
  40.  
  41. command = scanner.nextLine();
  42. }
  43. }
  44.  
  45. private static void printMatrix(String[][] matrix, int rows, int cols) {
  46. for (int row = 0; row < rows; row++) {
  47. for (int col = 0; col < cols; col++) {
  48. System.out.print(matrix[row][col] + " ");
  49. }
  50. System.out.println();
  51. }
  52. }
  53.  
  54. private static void fillMatrix(String[][] matrix, Scanner scanner) {
  55. for (int row = 0; row < matrix.length; row++) {
  56. matrix[row] = scanner.nextLine().split("\\s+");
  57. //"1 2 3".split(" ") -> ["1", "2, "3"]
  58. }
  59. }
  60.  
  61. private static boolean validateCommand(String command, int rows, int cols) {
  62. //true -> ако командата е валидна
  63. //false -> ако командата е невалидна
  64. //command = "swap row1 col1 row2 col2"
  65. String [] commandParts = command.split("\\s+");
  66. //"swap 1 2 3 4".split -> ["swap", "1", "2", "3", "4"]
  67.  
  68. //1. определен брой части -> 5
  69. if (commandParts.length != 5) {
  70. return false;
  71. }
  72.  
  73. //2. започва със swap
  74. if (!commandParts[0].equals("swap")) {
  75. return false;
  76. }
  77.  
  78. //3. дали редовете и колоните са валидни
  79. int row1 = Integer.parseInt(commandParts[1]); // >= 0 && < бр. редовете
  80. int col1 = Integer.parseInt(commandParts[2]); // >= 0 && < бр. колони
  81. int row2 = Integer.parseInt(commandParts[3]); // >= 0 && < бр. редовете
  82. int col2 = Integer.parseInt(commandParts[4]); // >= 0 && < бр. колони
  83.  
  84. if (row1 < 0 || row1 >= rows || row2 < 0 || row2 >= rows || col1 < 0 || col1 >= cols || col2 < 0 || col2 >= cols) {
  85. return false;
  86. }
  87.  
  88. return true;
  89.  
  90.  
  91. }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment