-1

actually I'm working in a batlleship project. And first I'm trying to make the program print the board. I have a for-loop (that includes another for-loop inside) and it does print the board with the values 10x10 like that:

  A B C D E F G H I J 
A - - - - - - - - - - 
B - - - - - - - - - - 
C - - - - - - - - - - 
D - - - - - - - - - - 
E - - - - - - - - - - 
F - - - - - - - - - - 
G - - - - - - - - - - 
H - - - - - - - - - - 
I - - - - - - - - - - 
J - - - - - - - - - - 

But, I'm trying to figure out how can I make the program to take the values NxN through keyboard inputs, in case that the user want different values. The min number of rows/columns being 5, the maximum being 26.

Bladimir Baez
  • 66
  • 1
  • 1
  • 6
  • 2
    I think Scanner would be the best for your idea: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – Tobias Horst Mar 01 '16 at 00:16

1 Answers1

0

Something like this should work...

Scanner in = new Scanner(System.In);
int width = readInt("width", in);
int height = readInt("height", in);
for (int x = 0; x < width; x++){
  for (int y = 0; y < height; y++){
    //...
  }
}
private int readInt(String label, Scanner in){
  System.out.println("Enter board " + label);
  return in.nextInt();
}
Hatward
  • 62
  • 7