I'm just starting out so I'm in the middle of writing my first console application from scratch. I have this line of code, when I hit d it correctly takes me to the next step and sets disadvantage to true, however if I hit a it executes the else statement for some reason. Any ideas what the cause is?
Console.WriteLine("Press the A key for advantage, or the D key for disadvantage");
var rollType = Console.ReadKey();
Console.WriteLine(System.Environment.NewLine);
if (rollType.Key == ConsoleKey.A)
{
advantage = true;
}
if (rollType.Key == ConsoleKey.D)
{
disadvantage = true;
}
else
{
Console.WriteLine("Invalid Input");
StartApp();
}
Just add make this small change! (Adding else
in your second conditional)
if (rollType.Key == ConsoleKey.A)
{
advantage = true;
}
else if (rollType.Key == ConsoleKey.D)
{
disadvantage = true;
}
else
{
Console.WriteLine("Invalid Input");
StartApp();
}
What was happening before is your Console would read an A key and enter the first conditional. Since the second and third conditional was separate from the first, the second would also be checked and if not true (which in this case it would not be true) it would no matter what enter the else statement. Hope this helps.