Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取字符数组中所有匹配字符的索引?_C#_Arrays_Visual Studio 2010_Foreach_Indexing - Fatal编程技术网

C# 如何获取字符数组中所有匹配字符的索引?

C# 如何获取字符数组中所有匹配字符的索引?,c#,arrays,visual-studio-2010,foreach,indexing,C#,Arrays,Visual Studio 2010,Foreach,Indexing,我正在做一个简单的刽子手程序。我的大部分代码都能正常工作,但我很难弄清楚如何获取字符数组中多个匹配字符的索引。例如,我将单词“sushi”转换为字符数组。如果用户猜到一个“s”,那么应该显示char数组中的所有“s”。我的代码是这样的,实际上我有两个长度相同的字符数组。第一个数组包含要猜测的单词的字符,而第二个数组包含问号。我的代码应该遍历第一个数组,并返回数组中与用户猜测匹配的每个元素的索引。然后,代码在每个指定索引处插入用户猜测,并为用户显示第二个数组。不幸的是,第二个数组中只更改了第一个匹

我正在做一个简单的刽子手程序。我的大部分代码都能正常工作,但我很难弄清楚如何获取字符数组中多个匹配字符的索引。例如,我将单词“sushi”转换为字符数组。如果用户猜到一个“s”,那么应该显示char数组中的所有“s”。我的代码是这样的,实际上我有两个长度相同的字符数组。第一个数组包含要猜测的单词的字符,而第二个数组包含问号。我的代码应该遍历第一个数组,并返回数组中与用户猜测匹配的每个元素的索引。然后,代码在每个指定索引处插入用户猜测,并为用户显示第二个数组。不幸的是,第二个数组中只更改了第一个匹配项,因此匹配查询只返回第一个索引。问题代码如下:

            //check if char array contains the user input
            if (guessThis.Contains(Convert.ToChar(textBox1.Text)))
            {
                //save user input as char
                char userGuess = Convert.ToChar(textBox1.Text);
                //iterate through first char array
                for (int i = 0; i < guessThis.Length - 1; i++ )
                {
                    //check each element in the array
                    //probably don't need both for and foreach loops
                    foreach (char c in guessThis)
                    {
                        //get index of any element that contains the userinput
                        var getIndex = Array.IndexOf(guessThis, c);
                        //check if the element matches the user guess
                        if (c == userGuess)
                        {
                            //insert the userguess into the index
                            displayAnswer[getIndex] = userGuess;

                        }
                    }

                }
                //update the display label
                answerLabel.Text = new string(displayAnswer);
因为你的:

var getIndex = Array.IndexOf(guessThis, c);
始终返回字符的第一次出现

您的第二个for循环和getIndex都是无用的,更清晰的代码可能是:

        for (int i = 0; i < guessThis.Length - 1; i++ )
        {
            //check if the element matches the user guess
            if (c == userGuess[i])
            {
                //insert the userguess into the index
                displayAnswer[i] = userGuess;
            }
        }
for(int i=0;i

此外,您的代码允许用户在textBox1中输入多个字符。我认为在这种游戏中,每次只能猜一个角色。所以我建议你限制你的投入

尝试
Regex.Matches
构建Regex表达式并查找所有匹配项及其位置

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "a*";
      string input = "abaabb";

      foreach (Match m in Regex.Matches(input, pattern)) 
         Console.WriteLine("'{0}' found at index {1}.", 
                           m.Value, m.Index);
   }
}
// The example displays the following output:
//       'a' found at index 0.
//       '' found at index 1.
//       'aa' found at index 2.
//       '' found at index 4.
//       '' found at index 5.
//       '' found at index 6.`

您可以这样做(为了清晰起见,将其设置为通用):

附加说明

顺便说一句,这可能与您的应用程序无关,但是.Net中的一些非ASCII Unicode字符实际上由表示。(还有修改前面角色的方法。)在“国际化”的刽子手游戏中,您可能希望通过将代理对转换为:

公共静态IEnumerable UTF32索引已执行的存储点(此字符串为s,int索引)
{
for(int-length=s.length;index
我认为问题在于方法: var getIndex=Array.IndexOf(c); 因为它返回第一个位置。
您只需使用for循环中的索引即可

尝试以下操作:

        char userGuess = Convert.ToChar(textBox1.Text);
        char[] displayAnswer = answerLabel.Text.ToCharArray();

        for (int n = 0; n < displayAnswer.Length; n++)
        {
            if (guessThis[n] == userGuess)
            {
                displayAnswer[n] = userGuess;
                }
        }
        answerLabel.Text = new string(displayAnswer);
char userGuess=Convert.ToChar(textBox1.Text);
char[]displayAnswer=answerLabel.Text.ToCharArray();
for(int n=0;n

一种简单的方法

默认情况下,应答标签上显示的文本是displayAnswer数组中包含的问号。因此,用户输入的内容实际上不匹配。我理解这是因为我发布的有限代码没有明确说明。否则,这可能会起作用。displayAnswer的相关代码是://循环数组中的每个元素(int i=0;i public static IEnumerable<int> AllIndexesOfAny<T>(this IList<T> list, IEnumerable<T> ofAny) { return Enumerable.Range(0, list.Count).Where(i => ofAny.Contains(list[i])); }
var matches = Enumerable.Range(0, guessThis.Length).Where(i => guessThis[i] == userGuess);
    public static IEnumerable<KeyValuePair<int, int>> Utf32IndexedCodePoints(this string s, int index)
    {
        for (int length = s.Length; index < length; index++)
        {
            yield return new KeyValuePair<int, int>(index, char.ConvertToUtf32(s, index));
            if (char.IsSurrogatePair(s, index))
                index++;
        }
    }
        char userGuess = Convert.ToChar(textBox1.Text);
        char[] displayAnswer = answerLabel.Text.ToCharArray();

        for (int n = 0; n < displayAnswer.Length; n++)
        {
            if (guessThis[n] == userGuess)
            {
                displayAnswer[n] = userGuess;
                }
        }
        answerLabel.Text = new string(displayAnswer);