para el bucle para la matriz de cadena de dos dimentional

Mohsen:

Quiero tener los siguientes resultados:

introducir descripción de la imagen aquí

y yo estaba escribiendo el siguiente código para este propósito:

String[][] teamView = new String[24][35];
    int [] numbers = new int[]{};
    int k =1;
    for(int i=0; i< 24; i++)
    {

        for(int j = 0 ; j< 35; j++)
        {
            if (j == 0 && i==0){teamView[i][j] = "@";}
            else if(j==0){ teamView[i][j] += (char)(i + 64) ;}
            else if (j==1){teamView[i][j] = " " ;}
            else if (i ==0 ){
                switch (j){
                    case 3:
                        teamView[i][j] = "1" ;
                        break;
                    case 6:
                        teamView[i][j] = "2" ;
                        break;
                    case 9:
                        teamView[i][j] = "3" ;
                        break;
                    case 12:
                        teamView[i][j] = "4" ;
                        break;
                    case 15:
                        teamView[i][j] = "5" ;
                        break;
                    case 18:
                        teamView[i][j] = "6" ;
                        break;
                    case 21:
                        teamView[i][j] = "7" ;
                        break;
                    case 24:
                        teamView[i][j] = "8" ;
                        break;
                    case 27:
                        teamView[i][j] = "9" ;
                        break;
                    case 30:
                        teamView[i][j] = "10" ;
                        break;

                }

            }else if (i ==0){teamView[i][j] = " ";}else


            teamView[i][j] = "#";
            System.out.print(teamView[i][j]);
        }
        System.out.print("\n");

    }

pero el problema es que ahora me sale nulo en el medio de los números en la primera fila, también antes de las letras de la primera columna. ¿Por qué recibo estos valores nulos en la impresión? ¿Cómo puedo mejorar mis bucles?

es sobre el juego acorazado en EDX ( https://courses.edx.org/courses/course-v1:PurdueX+CS180.4x+1T2020a/courseware/7e1459f3e5be4579b645cf16c4196954/a030e2346c374159b8875682791e1606/3?activate_block_id=block-v1%3APurdueX%2BCS180 .4x% 2B1T2020a% 2Btype% 40lti_consumer% 2Bblock% 408338de93e7c3499688734a1469b4eca9 ), si alguien tiene una idea me ayuda por favor, gracias.

B.Mik:

Los valores nulos en la primera línea están sucediendo porque tiene 2 repetidas sentencias condicionales if (i == 0)y los demás valores nulos en cada línea están sucediendo, ya que están concatenando el carbón con la cadena mediante +=el cual se da NULLA etc así que en vez de hacer esto:

String[][] teamView = new String[24][35];
        for (int i = 0; i < teamView.length; i++)
        {
            for (int j = 0; j < teamView[i].length; j++)
            {
                if (j == 0 && i == 0)
                {
                    teamView[i][j] = "@";
                } else if (j == 0)
                {
                    teamView[i][j] = String.valueOf((char) (i + 64)) ;  // the nulls on each line fix
                } else if (j == 1)
                {
                    teamView[i][j] = " ";
                } else if (i == 0 && j % 3 == 0)   //instead of the only i == 0 check its a multiple of 3
                {
                    teamView[i][j] = String.valueOf(j / 3);  //instead of the switch you can just divide the j by 3 if it's is a multiple of 3
                } else if (i == 0)
                {
                    teamView[i][j] = " ";
                } else
                    teamView[i][j] = "#";
                System.out.print(teamView[i][j]);
            }
            System.out.print("\n");
        }
    }

Salida

@  1  2  3  4  5  6  7  8  9  10  11 
A #################################
B #################################
C #################################
D #################################
E #################################
F #################################
G #################################
H #################################
I #################################
J #################################
K #################################
L #################################
M #################################
N #################################
O #################################
P #################################
Q #################################
R #################################
S #################################
T #################################
U #################################
V #################################
W #################################

Supongo que te gusta

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