在数组中找不到一些值

时间:2023-01-17 15:31:02

I have a array of strings, each string is built with the form "<x> <y>". if y starts on a 'n', it seems my program is not be be able to find it.

我有一个字符串数组,每个字符串都以“ ”的形式构建。如果y从'n'开始,似乎我的程序无法找到它。

So, the strings that don't work are.

所以,不起作用的字符串是。

"w north",
"w n",
"walk n",
"walk north"

Can you help to explain why?

你能帮忙解释一下原因吗?

string[] next = { "next", "ne", "nx", "nxt" };
string[] yes = { "yes", "y" };
string[] no = { "no", "n" };
string[] clear = { "clear", "c" };
string[] help = { "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 any input match the 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" and "walk north", should run this part of code:

字符串:“w north”,“w n”,“walk n”和“walk north”,应运行以下代码:

else if (walk.Any(input.Contains))
{
    MessageBox.Show( "test input" );
    Location C_locations = new Location();
    C_locations.Change_location( input );
    return "walk";
}

1 个解决方案

#1


4  

The reason your code does not work is in the content of your no array: it contains a single-letter string "n". It is this string that makes

你的代码不起作用的原因在于你的no数组的内容:它包含一个单字母字符串“n”。是这个字符串

no.Any( input.Contains )

evaluate to True for any input string that contains letter 'n'.

对于包含字母'n'的任何输入字符串,求值为True。

In order to fix this problem, you can move the checks for walk to the top of the if/then/else chain. However, the solution would not be overly robust: "yellow" would be classified as "yes", "cat" would become "clear", and so on.

为了解决这个问题,您可以将walk的检查移动到if / then / else链的顶部。但是,解决方案不会过于强大:“黄色”将被归类为“是”,“猫”将变为“清除”,依此类推。

#1


4  

The reason your code does not work is in the content of your no array: it contains a single-letter string "n". It is this string that makes

你的代码不起作用的原因在于你的no数组的内容:它包含一个单字母字符串“n”。是这个字符串

no.Any( input.Contains )

evaluate to True for any input string that contains letter 'n'.

对于包含字母'n'的任何输入字符串,求值为True。

In order to fix this problem, you can move the checks for walk to the top of the if/then/else chain. However, the solution would not be overly robust: "yellow" would be classified as "yes", "cat" would become "clear", and so on.

为了解决这个问题,您可以将walk的检查移动到if / then / else链的顶部。但是,解决方案不会过于强大:“黄色”将被归类为“是”,“猫”将变为“清除”,依此类推。