The task:
Create a program that introduces a square two-dimensional array of integers from the keyboard and replaces all elements lying on the main diagonal with the number of positive elements of the corresponding string, if there are any, or the maximum element if they are not. Print the resulting array to the screen.
Implementation:
package com.example.mas; public class arr6 { public static void main(String[] args) { int[][] a = {{4, 3, -1, -1}, {5, -2, 3, 1}, {0, 8, -9, 0}, {9, 5, 2, 0}}; int i, j, max, c; for (i = 0; i < 4; i++) { c = 0; max = a[i][0]; for (j = 0; j < 4; j++) { if (a[i][j] > 0) { c++; } if (a[i][j] > max) { max = a[i][j]; } if (a[i][j] < 0) { a[i][i] = c; } else { a[i][i] = max; } } } for (i = 0; i < a.length; i++) { for (j = 0; j < a.length; j++) { System.out.println(a[i][j]); } } } }