1
I’m trying to read a string typed into the console, which is halfway through the code, but what happens is it goes right through and displays the final information.
using System;
namespace Viernes
{
class Program
{
static void Main(string[] args)
{
int xDig = -1, yDig = -1;
char pCardeal = 'N';
//Verificando a validade
while (xDig < 0 && yDig < 0)
{
Console.Write("Digite as coordenadas \nx: ");
xDig = Convert.ToInt32(Console.ReadLine());
Console.Write("y: ");
yDig = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Cardeal 'N' 'S' 'L' 'O': ");
pCardeal = Convert.ToChar(Console.Read());
pCardeal = Char.ToUpper(pCardeal);
//Comandos
Console.WriteLine("Digite alguns comandos, 'E'squerda, 'D'ireita, 'A'vançar.");
string str = Console.ReadLine();
str = str.ToUpper();
char[] Comands = str.ToCharArray();
//Obj
SpaceCar sp = new SpaceCar(xDig, yDig, pCardeal);
foreach (var separaLetras in Comands)
{
switch (separaLetras)
{
case 'E':
sp.girarEsquerda();
break;
case 'D':
sp.girarDireita();
break;
case 'A':
sp.avancarBloco(xDig, yDig);
break;
}
}
//Imprimindo Destino
Console.Write("Coordenadas: {0}{1}{2}", sp.PosicaoX,sp.PosicaoY, sp.PosicaoCardial);
Console.WriteLine("Press Any Key to Exit");
Console.ReadKey();
}
}
}
Complement to class SpaceCar
in another Pastebin.
Hello. Why not edit your question and put the relevant code part into the question instead of pasting the link to the Pastebin?
– Omni
Your code doesn’t even compile. Where is the Spacecar class? If it is not absolutely necessary for the code, then you could take it out to post here. http://answall.com/help/mcve
– Maniero
The class is required. I put the code at the top and it works, but I need this insertion to be done in the middle of the code.
– Pegasus
If we can’t even test your code it’s hard to help. You have to put in complete, minimal, verifiable code. There are other problems that make your code even run. It would be good to give more details of how to reproduce the problem too.
– Maniero
Follow Spacecar code http://pastebin.com/Nw7qs6Pt
– Pegasus
on the line
pCardeal = Convert.ToChar(Console.Read());
the methodRead
does not wait for something to be typed, it picks up what has already been typed. Or you need to make a loop to pick up what will still be typed, or better yet, use theReadKey(flase)
in place, only then you will have to interpret the return that is of classConsoleKeyInfo
, ie returns the information of the key and not of the typed Character, needs a "conversion". If you find it too complicated and theReadLine
is enough, use it instead. It seems that this change solves the problem you have just described.– Maniero
It had already tested Readline E did not work Console.Writeline("Type some commands, 'E'squerda, ’D'ireita, 'A'vançar."); string str = Console.Readline(); str.Toupper(); char[] Comands = str.Tochararray();
– Pegasus
There is no reason for the
ReadLine()
not working. If it worked on others, it has to work on this.– Maniero
@Pegasus Take a look at [tour]. You can accept an answer if it solved your problem. You can vote for every post on the site as well. Did any help you more? You need something to be improved?
– Maniero