c# - Limiting input to just a, b, or d -


I am creating multiple choice quiz applications and I am trying to crack the verification. These options need to be limited in these options by selecting and holding the option between user, A, B, C or D. The problem is that the variables of these are string and must be string. The problem is that the program is accepting any string (as it should be). But is it just a way to limit these 4 options? I tried to force the program to catch up whether the letter no == A, B, C, or D but is not happy.

  string [] Quest Names; String [] QuestLevel; String [] quest; String [] questans; Quetsonms = new string [50]; Quest Level = New String [50]; Quest = new string [50]; QuestAns = new string [50]; While (level == "1") {Console.Clear (); Random rnd = new random (); Int level1_rand = rnd.Next (0, 9); AmountAsked ++; Get started: {Console.WriteLine ("\ n \ n Level: 1 \ t Score: {0} \ t Try the maximum balance: {1}", score, 20 - get the amount); Console.light line ("\ n \ n {0}", incorrect); Console.light line ("\ n \ n {0}", quest [level1_rand]); Console Type ("\ nNOTE:"); String Selection = Console. Readline (); String decider = QuestAns [level1_rand]; If (selection == decider) {level = "2"; Score = Score + 1; If (score> = 50) {pass (); }} If (Selection! = Decider) {fail = fail - 1; Wrong = wrong + 1; } If (selection! = Decisive & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; AmountAsked & gt; 19 || selection == decider & amp; amountAsked> 19 || fail == 0} {failed ( ); }} Hold {console.clear (); Console. Light line ("press any key to try invalid input, aigen"); Console.ReadLine (); Goto started; }}  

You should not use exceptions (i.e., to try / catch Block) Validate user inputInstitutes and throwing exceptions are not really cheap and use them in extraordinary, unpredictable cases (i.e., can not connect to the database, failed to save file on disk) should be done.

Generally, user input is not considered as an extraordinary, unpredictable case - users often consider it to be incorrect.

Instead, you can have a list of valid inputs, and check that the user's input is part of the list.

  using System.Linq; Static zero major () {var validInputs = new list & lt; String & gt; {"ABCD"}; String input = console Readline (); // Validate and retrying (! Valid input. Consumers) {Console.WriteLine ("The input was not valid, please try again."); Input = console Readline (); } / / Do something with valid input here}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -