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.
Perfect, solved. Can you tell me why?
– Giovane Machado
Readline read the typed string, already read read the typed character and return the next one (a hexadecimal conversation)
– HudsonPH
@Giovanemachado I edited, I think you can understand better with the edition
– HudsonPH
Actually this solution with
ReadLine()
still cause problems, this is correct: https://answall.com/a/140112/101. Can’t read more than one character withRead()
. Thisexemplo.ToString
does not make sense from the syntactic and semantic point of view.– Maniero