To output an array to the console, format the strings using the String.format or System.out.printf command simply by adding spaces to the number itself, for example System.out.printf("%4d", 15); will print " 15" , 4 characters, where the first 2 of 4 spaces. In your code, I fixed only the output to the console:
public static void main(String[] args) { System.out.print ("Введите размер треугольного правого массива "); triangle2(new Scanner(System.in).nextInt()); System.out.print("Введите размер перевернутого правого массива "); revTriangle2(new Scanner(System.in).nextInt()); } static int[][] triangle2(int lines) { int[][] arr = new int[lines][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; } int count = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j] = count++; } } for (int i = 0; i < arr.length; i++) { String str = ""; for (int j = 0; j < arr[i].length; j++) { str += String.format("%3d", arr[i][j]); } String format = "%" + lines * 3 + "s"; System.out.printf(format, str); System.out.println(); } return arr; } static int[][] revTriangle2(int lines){ int[][]arr = new int[lines][]; for(int i = 0; i < arr.length; i++){ arr[i] = new int[lines - i]; } int count = 0; for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = count++; } } for(int i = 0; i < arr.length; i++){ String str = ""; for(int j = 0; j < arr[i].length; j++){ str += String.format("%3d", arr[i][j]); } String format = "%" + lines * 3 + "s"; System.out.printf(format, str); System.out.println(); } return arr; }
Result in the console:
Введите размер треугольного правого массива 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Введите размер перевернутого правого массива 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
arr.length * arr[i].length, does not rearrange it and does not display the result anywhere. - iksuyarr[i][arr[i].length - 1- j] = count++;so want? - MrFylypenko