首页 > 解决方案 > Java寻宝游戏

问题描述

我有一个 CA,我必须在其中创建一个寻宝游戏,但我不知道如何将用户输入转换为网格位置,然后在网格中将其与用户输入一起显示。谁能给我一些帮助?我刚开始学习java,所以我是初学者,所以请多多包涵,哈哈。这是我迄今为止尝试使用的代码:

public void printBoard(){
    //*this method will actually print the board and fill it with the rows and columns labels 
    //and will be filled with the users' input: D if dug already or X if treasure been found

      board = new int [10][10];
      String[] nums = {"  1","  2","  3","  4","  5","  6","  7","  8","  9"," 10"};
     // char letters = 65;
      String[] letters = { "    A", "  B", "  C", "  D", "  E","  F", "  G", "  H", "  I", "  J"};
      for (int i = 0 ; i < board.length ; i++){

          System.out.print(letters[i]);
       //   letters++;
      }
      System.out.println("");

      for (int i = 0; i < board.length; i++) { 
          System.out.print(nums[i]);

      for (int j = 0 ; j < board.length ; j++){

          if (board[i][j] == 0) { //empty index 
            System.out.print("__|");
        } else if (board[i][j] == 1){ //when user input wont be an empty index so print d|
              System.out.println("d|");
        } else if(board[i][j] == -1) { //-1 already filled position with the treasure location so print x|

              System.out.println("x|");

        } 

public void getUserInput(){

       int userRow, userColumn;

        do{
            Scanner myScanner = new Scanner(System.in);
            System.out.println("Please enter row");
            userRow = myScanner.nextInt() -1;
            System.out.println("Please enter column");
            userColumn = myScanner.nextInt() -1;

            if(board[userRow][userColumn] == -1){
                System.out.println("That square has been used. Pick again");
            }

        }while (board[userRow][userColumn] != 0);

        if ((board[userRow][userColumn]) == 1){
        }
        else{
            board[userRow][userColumn] = 1;
        }

标签: java

解决方案


推荐阅读