0

String [] secretCode = null;

    String [] colores= {"Azul", "Amarillo", "Verde", "Naranja", "Rojo", "Rosa", "Blanco", "Gris"};
      secretCode = new String[5];

//the array of random colors has to contain 5 colors only.

        Random random = new Random();

            for (int i = 0; i < secretCode.length; i++) {
                int randomIndex = random.nextInt(colores.length);
                
                secretCode[i] = colores[randomIndex];
                System.out.println(secretCode);

//when i run this code it repeats the error 5 times.

  • 1
    Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) Another issue is that you probably intend to print out `secretCode[i]` inside the loop rather than the entire array `secretCode` each time through the loop. – President James K. Polk Jan 21 '23 at 01:34

1 Answers1

0
import java.util.Random; 
import java.util.Arrays;

class HelloWorld {
    public static void main(String[] args) {
        Random random = new Random();
        String[] colores= {"Azul", "Amarillo", "Verde", "Naranja", "Rojo", "Rosa", "Blanco", "Gris"};
        String[] secretCode = new String[5];

        for (int i = 0; i < secretCode.length; i++) {
            int randomIndex = random.nextInt(colores.length);
            secretCode[i] = colores[randomIndex];
            System.out.println(Arrays.toString(secretCode));                      
        }
    }
}
lexer31
  • 31
  • 3