I am trying to choose a string out of 4 strings randomly, and to show this string on the console. How can i do it ? For example, there is a question, if user answers it correctly, then the console will display one of the strings that i chose. I know how to choose an integer value randomly, but i could not figure out how to choose a string randomly. Please Help?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Using charAt:
Use that integer value that you select randomly as the index for your array of strings.
Random
class that's within the bounds of the length of your array (look at the modulo%
operator to figure out how to do this; alternatively, constrain the call to random.nextInt() by passing an uppper bound).this will generate a random number between 0 [inclusive] and list.size() [non-inclusive]. Then, just get that element at that index out of the list.