2014-10-13 70 views
-1

所以我试图从数组中创建一个索引来绕过我已有的问号网格。我试图让它输出每列和网格的每一行的数量,但我无法让它正确地绕过网格。有没有人有我如何做到这一点的想法?Java数组索引

import java.util.Scanner; 
import java.util.Arrays; 

public class H4_Minesweeper { 

    public static void main(String[] args) { 
     //Game Description and rules 
     System.out.println("Minesweeper is a very straightforward game, the rules are simple."); 
     System.out.println("Uncover a mine (x), and the game ends. Uncover an empty square (o), and you keep playing."); 
     System.out.println("A question mark (?) will represent tiles you have not uncovered yet."); 
     System.out.println("Uncover a number, and it tells you how many mines lay hidden in the eight surrounding squares."); 
     System.out.println("To enter which tile you would like to uncover, please enter the column number, then row number of that tile."); 
     System.out.println("\n\n"); 

     Scanner userin = new Scanner(System.in); 

     //Index of the grid 
     int[] column = {1,2,3,4,5,6,7,8,9}; 
     for (int counter = 0; counter<column.length;counter++){ 
      System.out.print(counter); 
     } 

     //9x9 grid output 
     String[][] board = new String [9][9]; 
     for (int r = 0; r<board.length;r++){ 
      for (int c = 0; c <board.length;c++){ 
       { 
       } 
       board [r][c] ="?"; 

       System.out.print("[" + board[r][c] + "]"); 
       } 

       System.out.println(); 
     } 
     //User input for column & row 
     String move = userin.nextLine(); 
    } 
} 

回答

0

它看起来像要打印的列数,但没有行号。我建议这种变化:

System.out.print(" "); 
    int[] column = {1,2,3,4,5,6,7,8,9}; 
    for (int counter = 0; counter<column.length;counter++){ 
     System.out.print(counter); // print column number 
    } 
    System.out.println(""); 
    System.out.println(""); // empty row 

    //9x9 grid output 
    String[][] board = new String [9][9]; 
    for (int r = 0; r<board.length;r++){ 
     System.out.print(r+" "); // print row number 
     for (int c = 0; c <board.length;c++){ 

      board [r][c] ="?"; 

      System.out.print(board[r][c]); // print cell value 
      } 

      System.out.println(""); 
    } 

这应该打印



0 ????????? 
1 ????????? 
2 ????????? 
3 ????????? 
4 ????????? 
5 ????????? 
6 ????????? 
7 ????????? 
8 ????????? 

希望,这就是你的意思。如果您仍想将?打印为[?],则必须在列号之间添加额外的空格。