0

I wrote out a list filled with answers for a question. I want to use my GetAnswer() method to return the current answer as a string.

The issue that I'm having is that I can't get the random answer that I selected to print out.

namespace Magic8Ball_Logic
{

public class Magic8Ball
{
    private List<string> _answers;
    private int _currentIndex;
    public Magic8Ball()
    {
        _answers = new List<string>();
        _answers.Add("It is certain.");
        _answers.Add("It is decidedly so.");
        _answers.Add("Without a doubt.");
    }

    public Magic8Ball(List<string> answers)
    {
        //I won't use the default. Use the ones passed in.
        _answers = answers;
    }

    public void Shake()
    {
        //picking the index of the answer to show the user
        Random r = new Random();
        int index = r.Next(_answers.Count);
        string randomString = _answers[index];
    }

    public string GetAnswer()
    {
        //using the index picked by shake to return the answer
        return randomString;
    }
  • *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* – Ňɏssa Pøngjǣrdenlarp May 04 '19 at 15:30
  • 1
    Remove your `GetAnswer` method and let `Shake` return `_answers[index]` or in `Shake` save the index as a class variable and `GetAnswer` will return `_answers[_index]` – Deolus May 04 '19 at 15:34
  • The `randomString` is a local variable to the `Shake` method. It isn’t accessible from outside. Move it to the class fields – Raul May 04 '19 at 15:38
  • Btw. Depending on how often you "Shake", once you fix this you may find that [creating a Random object each time isn't helping you](https://stackoverflow.com/a/24753931/1734730). – Nathan Cooper May 07 '19 at 07:57

3 Answers3

1

Tell me,if you don't understand something.

   public Form1()

    {
        InitializeComponent();
        _answers.Add("It is certain.");
        _answers.Add("It is decidedly so.");
        _answers.Add("Without a doubt.");
    }

    List<string> _answers = new List<string>();

    private void BtnRandom_Click(object sender, EventArgs e)
    {
        MessageBox.Show(GetAnswer());

    }
    string GetAnswer()
    {
        Random rnd = new Random();
        int i = 0;
        int _rnd = rnd.Next(_answers.Count);
        foreach (string answer in _answers)
        {
            if (i == _rnd)
            {
                return answer;
            }
            i++;
        }

        return "";
    }
}
AleMaca
  • 11
  • 4
0

Using your original code:

public void Shake()
{
    //picking the index of the answer to show the user
    Random r = new Random();
    _currentIndex = r.Next(_answers.Count);
}

public string GetAnswer()
{
    //using the index picked by shake to return the answer
    return _answers[_currentIndex];
}

You might need to make your Random static. There are other threads that you can refer to for this.

Deolus
  • 317
  • 5
  • 13
-1

you can do this by using the Random class that comes with the system. Make sure you have using System; at the top of your code. If you want to avoid adding this line at the top of your code, you can just add System. before every time you see the word "Random".

Random rnd = new Random();
return _asnswers[rnd.Next(0, _answers.Count);

By the way, try to avoid using variables with '_' at the beginning because it is used for variables from another type, such as:

using _StartWith = System.File;

Name the variables "answers" (preferred) or "Answers". The code will work 100% but this is just for when other people look at your code they will know what variable is what type.

Also, I assume you want to shuffle the answers so I will help you with it too:

Random rnd = new Random();
for (int i = _answers.Count - 1; i >= 0; i++)
{
    _answers.Add(    // Add the random removed answer
    _answers.Remove( // Removes the random answer
    rnd.Next(0, i)));// Randomizes a random answer in a range of 0 to the number of answers that didn't get picked before.
}

Good luck! Tell me if I helped :)

rom_totach
  • 106
  • 8