79537093

Date: 2025-03-26 19:32:02
Score: 1.5
Natty:
Report link

As has been said in the comments by @Slaw and @Anonymous:

The algorithm that your teacher is after is (partly pseudocode):

while (remainingElements > 0) {
    int i = <random index>;
    if (board[i] != 0) {
        print board[i];
        board[i] = 0;
        remainingElements--;
    }
}

For picking a random index into your board you need to use random.nextInt(board.length).

Notice that we only decrease remainingElements when a number is picked and printed, not every time through the loop. This causes the loop to repeat more times than there are elements in the board array. Eventually the random index will also pick the last remaining elements, remainingElements will be decreased to 0 and the loop will terminate.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • User mentioned (1): @Slaw
  • User mentioned (0): @Anonymous
  • Low reputation (1):
Posted by: Lily Devi