I am trying to create a battleship game using arraylist .
The program was meant to first ask how many cells the user wants in the game,then it itself creates coordinates of three ships and ask user input for hitting the ship.
The problem is when i run the code and i wrote a loop to check the co-ordinates of the randomly generated co-ordinates i am getting all co-ordinates as 0,0 for all the ships.
vamsi naki wrote:i am getting all co-ordinates as 0,0 for all the ships.
I think everybody else has covered the why, but (and this is a general tip) if you want random numbers, you're far better off to use the java.util.Random class.
1. Because it was written specifically to produce random numbers. Math.random() is just one method in a collection of maths utilities.
2. It's Thread-safe.
3. Math.random() actually uses java.util.Random (as of Java 1.4, I think), so you have the overhead of an additional call.
And the method that you probably need to look at is Random.nextInt(int).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here