Switch only enters the "default"

0

When I read a number, converting it to Int32 , my switch simply understands that it is a default and makes the code described there.

static void Main(string[] args)
    {
        Jogador j = new Jogador(); //instancia que representa as decisões do jogador
        Jogador npc = new Jogador(); //instancia que representa o adv controlado pelo computador
        Random rdn = new Random(); // pra gerar a jogada do NPC
        int jj = 1;

        Intro();
        while (jj != 4)
        {
            Opcoes();
            int jnpc = Convert.ToInt32(rdn.Next(1, 3)); //gerando numero aleatorio da jogada do NPC
            jj = Convert.ToInt32(Console.Read());

            switch (jj) // <<<<<<<<<<<<<<- aqui ele le como default qualquer coisa digitada
            {
                case 1: //pedra
                    if (jj == jnpc)
                    {
                        j.Empatou();
                        npc.Empatou();
                        Console.WriteLine("EMPATE");
                    }
                    else if (jnpc == 2)
                    {
                        j.Perdeu();
                        npc.Venceu();
                        Console.WriteLine("VITÓRIA do Computador");
                    }
                    else if (jnpc == 3)
                    {
                        j.Venceu();
                        npc.Perdeu();
                        Console.WriteLine("VITÓRIA do Jogador");
                    }
                    break;
                case 2: //papel
                    if (jj == jnpc)
                    {
                        j.Empatou();
                        npc.Empatou();
                        Console.WriteLine("EMPATE");
                    }
                    else if (jnpc == 1)
                    {
                        j.Venceu();
                        npc.Perdeu();
                        Console.WriteLine("VITÓRIA do Jogador");
                    }
                    else if (jnpc == 3)
                    {
                        j.Perdeu();
                        npc.Venceu();
                        Console.WriteLine("VITÓRIA do Computador");
                    }
                    break;
                case 3: //tesoura
                    if (jj == jnpc)
                    {
                        j.Empatou();
                        npc.Empatou();
                        Console.WriteLine("EMPATE");
                    }
                    else if (jnpc == 1)
                    {
                        j.Perdeu();
                        npc.Venceu();
                        Console.WriteLine("VITÓRIA do Computador");
                    }
                    else if (jnpc == 2)
                    {
                        j.Venceu();
                        npc.Venceu();
                        Console.WriteLine("VITÓRIA do Jogador");
                    }
                    break;
                case 4:


                    break;
                default:
                    Console.WriteLine("Voce digitou um valor inválido, tente novamente");
                    break;
            }//fim do switch
        }//fim do while

In order not to lose the clutter it also plays 3 times part of while and then goes to read again. From the situation, I believe it might be this: My conversion should be another way to% understand the number (I tested it by putting an integer there and then it worked)

And about playing part of the code, I can not understand why, after all it reads switch , which is just a method to shorten text (it has no function other than writing on the screen), but it does not execute opcoes() that has just below.

The image below is what happens when I type any number.

    
asked by anonymous 19.02.2018 / 14:44

2 answers

3

That:

jj = Convert.ToInt32(Console.Read());

Switch to this:

Convert.ToInt32(Console.ReadLine());

If you want to use Console.Read() you need to convert the value to char .

jj = Console.Read();
char exemplo = Convert.ToChar(jj);
switch (exemplo.ToString)

If the user types multiple values into the conversion, wrath returns only the first char

Best solution:

Int32.TryParse(Console.ReadLine(), out jj);
19.02.2018 / 15:00
6

Just do this:

jj = Console.Read() - 48;

The character 0 is the 48, so doing this account does not need to convert anything, it does not complicate, and works as desired.

If you want more semantics you can (char)'0' instead of 48.

    
19.02.2018 / 15:41