我知道我可以使用嵌套的for循环创建行和列

最佳答案

由于您无法使用数组,因此无法创建任何新方法。您可以执行以下操作:

输出:

 O  .  .  .  .
 .  .  .  .  .
 .  .  .  .  .
 .  .  .  .  .
 .  .  .  .  E

代码:
    int playerX=0, playerY=0;  //holds player's location
    int exitX = 4, exitY = 4;  //holds exit's location

    //Print map
    for(int x=0; x<5; x++){
        for(int y=0; y<5; y++)
            if(x == playerX && y == playerY)
                System.out.print(" O "); //print player location
            else if(x == exitX && y == exitY)
                System.out.print(" E ");
            else
                System.out.print(" . ");
        System.out.println("");
    }
    /*
    //Update player's position on movement
    if (movement == DOWN)
        playerY = Math.min(playerY+1 , 5);
    else if (movement == UP)
        playerY = Math.max(playerY-1 , 0);
    else if (movement == LEFT)
        playerX = Math.max(playerX-1 , 0);
    else else if (movement == RIGHT)
        playerX = Math.max(playerY+1 , 5);
    */

您可以将所有内容括在while循环中,然后重复该循环,以便玩家的位置与出口的位置不同。

09-11 18:40