Noob here. I've tried using the other answers for random numbers, but they don't seem to accomplish what I'm trying to do.
So... I'm trying to make a method that catches a random fish in a pond. It will catch multiple fish in a row that are stored in an array, and I basically want it to avoid catching null
, and still catch a fish every time it is run. Here's the method.
public String catchAFish(){
int x = (int)(numFish * Math.random());
pond[x] = pond[numFish+1];
pond[numFish+1] = null;
numFish--;
return ("You have caught "+pond[x]);
}
The last fish in the array moves into the spot of the fish that was caught. I tried a function in a for
loop before, and it accomplished the same thing. The problem is that the number of possible fish to catch changes whenever a fish is caught, so I can't just set a range of "0-10".