二维String数组的for循环

时间:2020-03-28 18:18:02

标签: java arrays for-loop multidimensional-array

我希望得到以下结果:

enter image description here

为此,我正在编写以下代码:

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");

    }

但是问题在于,现在我在第一行中的数字之间,以及第一列中的字母之前,都得到了null。为什么我在打印中得到这些空值?我如何改善循环?

它与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)中的战舰游戏有关,如果有人有想法请帮助我,谢谢。

2 个答案:

答案 0 :(得分:0)

由于您有2个重复的条件语句unpack_trees.c,因此第一行出现了空值,而由于使用lstat()将char与字符串连接在一起,使得每一行中的其他null都出现了, nullA等等,而是这样做:

unpack_trees

输出

--recurse-submodules

答案 1 :(得分:0)

您不需要数组或如此复杂的逻辑。您只需两个循环即可完成此操作:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (i == 0 && j == 0) {
                    System.out.print((char) (j + 64) + " ");
                } else if (i == 0) {
                    System.out.print(j + " ");
                } else if (j == 0) {
                    System.out.print((char) (i + 64) + " ");
                } else {
                    System.out.print("# ");
                }
            }
            System.out.println();
        }
    }
}

输出:

@ 1 2 3 4 5 6 7 8 9 10 
A # # # # # # # # # # 
B # # # # # # # # # # 
C # # # # # # # # # # 
D # # # # # # # # # # 
E # # # # # # # # # # 
F # # # # # # # # # # 
G # # # # # # # # # # 
H # # # # # # # # # # 
I # # # # # # # # # # 
J # # # # # # # # # # 

逻辑很简单。如有任何疑问/问题,请随时发表评论。

相关问题