c# - Can't find some values in array -
i have array of strings, each string built form "<x> <y>"
. if y
starts on 'n'
, seems program not be able find it.
so, strings don't work are.
"w north", "w n", "walk n", "walk north"
can explain why?
string[] next = { "next", "ne", "nx", "nxt" }; string[] yes = { "yes", "y" }; string[] no = { "no", "n" }; string[] clear = { "clear", "c" }; string[] = { "help", "h" }; string[] walk = { "w north", "w south", "w west", "w east", "w n", "w s", "w w", "w e", "walk north", "walk south", "walk west", "walk east" , "walk n", "walk s", "walk w", "walk e" }; //checks if input match arrays public string input(string input) { input = input.tolower(); if (next.any(input.contains)) { return "next"; } else if (yes.any(input.contains)) { return "yes"; } else if (no.any(input.contains)) { return "no"; } else if (clear.any(input.contains)) { return "clear"; } else if (help.any(input.contains)) { return "help"; } else if (walk.any(input.contains)) { messagebox.show("test input"); location c_locations = new location(); c_locations.change_location(input); return "walk"; } else { return "not found"; } }
the strings: "w north"
, "w n"
, "walk n"
, "walk north"
, should run part of code:
else if (walk.any(input.contains)) { messagebox.show( "test input" ); location c_locations = new location(); c_locations.change_location( input ); return "walk"; }
the reason code not work in content of no
array: contains single-letter string "n"
. string makes
no.any( input.contains )
evaluate true
input string contains letter 'n'
.
in order fix problem, can move checks walk
top of if
/then
/else
chain. however, solution not overly robust: "yellow"
classified "yes"
, "cat"
become "clear"
, , on.
Comments
Post a Comment