Switch only enters "default"

Asked

Viewed 170 times

0

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

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

Not to lose his temper he also plays 3 more times part of the while and then goes to read again. From the situation, I believe this could be it: My conversion should be another pro switch understand the number (I tested by placing an integer there and then it worked)

And about being reproducing part of the code, I can’t understand why, after all, it reads the opcoes(), which is only a method for shortening text (has no function other than writing on the screen), but does not execute the read that has just below.

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

Visual Studio com o código

2 answers

6

Just do this:

jj = Console.Read() - 48;

I put in the Github for future reference.

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

If you want more semantics you can do (char)'0' in place of 48.

3


That:

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

Trade for this:

Convert.ToInt32(Console.ReadLine());

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

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

If the user type several values the conversation will return only the first char


Best solution:

Int32.TryParse(Console.ReadLine(), out jj);

  • Perfect, solved. Can you tell me why?

  • 1

    Readline read the typed string, already read read the typed character and return the next one (a hexadecimal conversation)

  • 1

    @Giovanemachado I edited, I think you can understand better with the edition

  • 3

    Actually this solution with ReadLine() still cause problems, this is correct: https://answall.com/a/140112/101. Can’t read more than one character with Read(). This exemplo.ToString does not make sense from the syntactic and semantic point of view.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.