Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将2d数组打印为网格,并随机选择一个作为胸部,然后将该值重命名为C_Java_Arrays_2d - Fatal编程技术网

Java 将2d数组打印为网格,并随机选择一个作为胸部,然后将该值重命名为C

Java 将2d数组打印为网格,并随机选择一个作为胸部,然后将该值重命名为C,java,arrays,2d,Java,Arrays,2d,我现在有它,所以它打印了一个2乘2的网格,并将我的位置重命名为p,其他所有的都重命名为-它隐藏胸部,直到我踩到它,问我是否要打开胸部,然后结束游戏,但我希望它将随机生成的胸部重命名为C,以便我可以在网格上看到它的位置,有人能帮我解决这个问题吗 /** * Auto Generated Java Class. */ import java.util.*; public class Adventure { public static

我现在有它,所以它打印了一个2乘2的网格,并将我的位置重命名为p,其他所有的都重命名为-它隐藏胸部,直到我踩到它,问我是否要打开胸部,然后结束游戏,但我希望它将随机生成的胸部重命名为C,以便我可以在网格上看到它的位置,有人能帮我解决这个问题吗

        /**
     * Auto Generated Java Class.
     */
    import java.util.*;
    public class Adventure {

      public static final int rows= 2;
      public static String tile = "";
      public static final int cols= 2;
      public static String input = "";
      public static boolean run = true;
      public static String[][] map = new String[rows][cols];
      public static int xpos = 0;
      public static int ypos = 0;
      public static Random gen = new Random();
      public static final  int xx = gen.nextInt(rows);
      public static final  int yy = gen.nextInt(cols);



      public static void main(String[] args) { 



        for(int x = 0; x < rows; x++) {
          for(int y = 0; y < cols; y++) {
            map[x][y] = "you see nothing, on the fields of justice";
          }
        }


        map[xx][yy] = "You find a chest: Open?";


        while(run) {
          displayMap();

          System.out.println(map[xpos][ypos]);

          input = userinput();
          input = input.toLowerCase();

          if(input.equals("w")) {
            if (ypos < cols - 1) {
              ypos++;
            }else{
              System.out.println("You're at the edge of the map");
            }
          }else if(input.equals("d")) {
            if (xpos < rows - 1) {
              xpos++;
            }else{
              System.out.println("You're at the edge of the map");
            }

          }else if(input.equals("s")) {
            if (ypos > 0) {
              ypos--;
            }else{
              System.out.println("You're at the edge of the map");
            }

          }else if(input.equals("a")) {
            if (xpos > 0) {
              xpos--;
            }else{
              System.out.println("You're at the edge of the map");
            }

          }else if(input.equals("open")) {
            if(map[xpos][ypos].equals("You find a chest: Open?")){
              System.out.println("You find a sword");
              System.out.println("()==[:::::::::::::>");
              run = false;

            }else{
              System.out.println("There's nothing to open");
            }
          }else {
            System.out.println("Wrong direction, W , A, S , D");
          }

        } 
      }
      public static String userinput(){
        System.out.println("Which direction do you want to move: w , a ,s ,d ? ");
        Scanner keyboard = new Scanner(System.in);
        return keyboard.nextLine();
      } 





      public static void displayMap() {


        for (int y = cols - 1; y >= 0; y--) {
          for (int x = 0; x < rows; x++) {

            if (y == ypos && x == xpos) {
              tile = tile + "P";

            }


//Heres where i want to rename the chest to 'C' on the grid
            else if (xx && yy) {
              tile = tile + "C";
            }
            else{
              tile = tile + "-";
            }
      }
      System.out.println(tile);
      tile = "";
    }
  }
}

如果y==yy&&x==xx,则用else替换xx&&yy中的else。这是一个容易犯的错误

谢谢!我知道这很简单,但我看不见