jeudi 24 novembre 2022

How to return one item from this list?

I need to use the nextInt method of Random to select and return one item from the list. I need the method to return any of the strings in the list, regardless of the size of the list. However, the list will always have at least one item in it.

Here is my method at the moment but it gives me errors that variable choices is already defined in this method and variables ArrayList and String are undeclared.

    /**

    * Return a random one of the strings in the given list.

    * The list will have at least one item in it.

    * @param choices A list of strings.

    * @return One of the strings in the given list.

    */

    public String chooseOne(ArrayList<String> choices)

    {

    Random rand = new Random();

    ArrayList<String>choices;

    return rand.nextString(ArrayList<String>choices);



Aucun commentaire:

Enregistrer un commentaire