Random words

-1

I would like to know how to make random words stored in my database:

For example, I already tried to save letters from 1 to 7 with the following method:

    rand(1,7);

My question is: How can you write this function but save 7 different words or sentences at random?

For example: dog, cat, hello you do, good and you, program, another word, chance

Here are 7 example words: How do I get one of those randomly?

    
asked by marianorz 17.07.2018 в 01:09
source

1 answer

0

If you want to select one of those phrases at random, you can do it like this:

import java.util.Random;

public class MyClass {
    public static void main(String args[]) {
        String[] x = {"perro", "gato", "hola que haces", "bien y tu", "programar", "otra palabra", "azar"};
        Random rand = new Random();

        int n = rand.nextInt(7);
        String z = x [n];

        System.out.println(z);
    }
}
    
answered by 17.07.2018 в 01:18