V
V
vladimir_html2018-06-01 18:33:24
C++ / C#
vladimir_html, 2018-06-01 18:33:24

I'm struggling with variables.?

newbie doing a simple rock-paper-scissors console

Console.WriteLine("Го играть в камень ножницы бумага? Что выберешь?");
            string userChoice = Console.ReadLine();
            Console.WriteLine($"{userChoice} , значит?");

            Random random = new Random();
            int aiNumberChoice = random.Next(3);
            switch (aiNumberChoice)
            {
                case 0:
                    string aiChoice = "Камень";
                    break;
                case 1:
                    aiChoice = "Ножницы";
                    break;
                case 2:
                    aiChoice = "Бумага";
                    break;
            }
            Console.WriteLine("Компьютер выбрал " + aiChoice);
         // дальше должен быть свитч, сравнение ответа пользователя с АИ.

5b1167135b323529723888.png
VS swears at the aiChoice variable, saying that there is no such one. As I understand it, this variable appeared in the switch and was destroyed there, right? and how then to transfer its value out of a switch?
do not throw slippers, I'm just a teapot

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2018-06-01
@vladimir_html

string aiChoice
declare not in a switch construct. And a little higher. Well, for example, up to this line.
Something like this should be

static void Main(string[] args)
        {
            string aiChoice = String.Empty;
            Console.WriteLine("Го играть в камень ножницы бумага? Что выберешь?");
            string userChoice = Console.ReadLine();
            Console.WriteLine($"{userChoice} , значит?");

            Random random = new Random();
            int aiNumberChoice = random.Next(3);
            switch (aiNumberChoice)
            {
                case 0:
                    aiChoice = "Камень";
                    break;
                case 1:
                    aiChoice = "Ножницы";
                    break;
                case 2:
                    aiChoice = "Бумага";
                    break;
            }
            Console.WriteLine("Компьютер выбрал " + aiChoice);
            Console.ReadKey();
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question