❔ ✅ While loop takes input but gives out output + two phantom outputs
working on a simple rock, paper, scissor game. After a few tries with if statements, I changed to switch statements, but both have the same problem.
Code looks like this: https://paste.ofcode.org/TWijGyx4rzUSdgdVi4mWTS
running it looks like this:
https://gyazo.com/42a2a8891da011b6ca32ec5e2eaf49ff
The two extra prompts + "invalid input" happen all the time aside from choosing 'e'. I assume that's because I've specifically put
stopPlaying = true
there. However, it shouldn't touch the default case when the input is valid and it definitely shouldn't repeat the prompt + default twice at all after processing the input.
What am I missing here?Gyazo
Gyazo
9 Replies
(this actually took me a while)
Okay so you're using
Console.Read()
, which reads one character at a time from the console's input stream
Issue with that is that when it asks you to enter a character, you type a character + enter, which enters three characters: the character you typed, \r
, and \n
(which are the two newline characrers on Windows).
You should probably use playerChoice = char.ToLower(Console.ReadLine()[0])
instead
Which reads an entire line from the user and takes the first character from itAh, okay.
Would .ReadKey work instead or what would that do?
Yeah that too
Console.ReadKey().KeyChar
I see.
Also, since I'm new to switch cases, is there a way to branch them? I'm trying to see if I can be a bit more elegant than big if statements
wdym branch them?
I know I can check multiple ifs at the same time with stuff like https://paste.ofcode.org/P6baM3DpBydj6gk9hu9aXD
But is there a way to include that check against the computer in a switch statement?
you could do this:
or if you want something a bit longer
Oh nice! Thank you!
!close
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.