Como llegar a la suma de la fila 2 matriz bidimensional y les rango de mayor a menor?

luces:

Tengo aquí el código para obtener la suma fila, pero la respuesta es incorrecta, y no sé cómo llegar a las filas.

int[][] ranks = {
      {29, 20, 7, 25, 32, 6}, 
      {20, 31, 17, 31, 32, 26},
      {22, 30, 16, 32, 22, 15}
};
int sum = 0;
for(int i = 0; i < 3; i++ )
{
for(int j = 0; j < 6; j++)
{
    sum = sum + ranks[i][j];
}
System.out.println(sum);    
        }
Arvind Kumar Avinash:

Hacerlo de la siguiente manera:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] ranks = { { 29, 20, 7, 25, 32, 6 }, { 20, 31, 17, 31, 32, 26 }, { 22, 30, 16, 32, 22, 15 } };
        int sum[] = new int[ranks.length];
        for (int i = 0; i < ranks.length; i++) {
            for (int j = 0; j < ranks[i].length; j++) {
                sum[i] += ranks[i][j];
            }
            System.out.println("Sum of row " + (i + 1) + " = " + sum[i]);
        }
        Arrays.sort(sum);
        System.out.println("Sum of ranks in highest to lowest: ");
        for (int i = sum.length - 1; i >= 0; i--) {
            System.out.println(sum[i]);
        }
    }
}

Salida:

Sum of row 1 = 119
Sum of row 2 = 157
Sum of row 3 = 137
Sum of ranks in highest to lowest: 
157
137
119

notas:

  1. Utilice un intarray ( int sum[]en el código dado anteriormente) para almacenar la suma de cada fila.
  2. El valor predeterminado en cada índice de un intarray es 0.
  3. Para ordenar una matriz en orden ascendente, se puede utilizar java.util.Arrays::sort. Puede haber muchas otras maneras también.
  4. Mostrar la matriz ordenada del último al primer elemento con el fin de mostrar los números de mayor a menor.
  5. Por último, pero no menos importante, el uso lengthde atributos de la matriz en lugar de utilizar números fijos como 3o 6para limitar las iteraciones.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=365558&siteId=1
Recomendado
Clasificación