C
C#2y ago
Ronnie

Forcing user to choose 1, 2 or 3

Hello, im a bit lost! first is a while-loop to make sure input is not null, but then how would I best structure this to make it not move on if input is not 1, 2 or 3? Switch didnt work, since default; sends user back to the menu instead of changing the input
11 Replies
Ronnie
RonnieOP2y ago
is there any way to make this a part of the while loop? so that it will enter loop not only if TryParse but also if number is not one two or three?
ero
ero2y ago
&& (bokTyp < 1 || bokTyp > 3)
Ronnie
RonnieOP2y ago
Ronnie
RonnieOP2y ago
did i put it incorrectly? it moves on to the switch anyways
ero
ero2y ago
Ah, it's || !(bokTyp >= 1 && bokTyp <= 3) Other way around
Ronnie
RonnieOP2y ago
Still not working, not getting any error messages but it doesnt seem to be checking the second condition before moving on from the while
Shinigami
Shinigami2y ago
While(boktype <= 3 && boktype >=1) This should work (!value.IsNullOrEmpty(input)) You can use this to check for null or empty
Ronnie
RonnieOP2y ago
I was missing one set of ()! now it does check the second condition but instead, doesnt move on despite the number being 1 2 3 :p
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
string input = "4";

if (!int.TryParse(input, out int x) || x < 1 || x> 3)
Console.WriteLine("foo");
string input = "4";

if (!int.TryParse(input, out int x) || x < 1 || x> 3)
Console.WriteLine("foo");
Console Output
foo
foo
Compile: 574.279ms | Execution: 82.951ms | React with ❌ to remove this embed.
Ronnie
RonnieOP2y ago
Thank you so much!! success
Ronnie
RonnieOP2y ago
Want results from more Discord servers?
Add your server