Set of 3 Java ports

0

I have to do an exercise in Java that consists of creating an array [3] [3] where each line would be a phase of the game. The user must guess which door the prize is on. For this I need to do a random on each line assigning the value 1 for the premium port and 0 for the others, the question is how do I / p>     

asked by anonymous 17.04.2015 / 11:45

1 answer

4

The java.util.Random class has a method nextInt(int) that returns an integer in the [0,n[ range, given an input% n . Using 3 , it will return a number in [0,1,2] . Then just assign the port with that number to 1 , and keep the others with 0 :

Random r = new Random();
// para cada linha
int qual = r.nextInt(3);
linha[qual] = 1;
    
17.04.2015 / 11:59