Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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# - Fatal编程技术网

C# 将一个字符串[索引]与另一个字符串进行比较

C# 将一个字符串[索引]与另一个字符串进行比较,c#,C#,我正在学习C#,我正在从头开始制作一个刽子手游戏,作为我的第一个项目之一 for (int i = 0; i <= answer.Length; i++) //answer is a string "theword" { if (answer[i] == passMe) //passMe is "A" for example { hiddenWord = hidd

我正在学习C#,我正在从头开始制作一个刽子手游戏,作为我的第一个项目之一

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
除了用正确猜出的字母替换隐藏单词的破折号的部分外,其他部分都可以工作

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
例如:-在你猜到G、E和A之后变成G-EA

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
我有一个for循环,逻辑上似乎它可以完成这项工作,但我不能对字符串或字符使用==运算符

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }

for(int i=0;i如果
passMe
是一个只有一个字符的字符串,那么

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
if (answer[i] == passMe[0])
通过这种方式,您可以将用户输入的第i个位置的字符与第一个位置的字符进行比较

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
代码中还有一个严重错误。 你的循环偏离1,将其更改为

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
for (int i = 0; i < answer.Length; i++)
for(int i=0;i

NET中的数组从索引0开始,并且最大索引值总是比数组的长度小一个。

answer[i]指的是一个字符,passMe指的是一个字符串。(不是一个字符)

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
试试这个

for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }
for (int i = 0; i <= answer.Length; i++) //answer is a string "theword"
            {
                if (answer[i] == passMe[0]) //passMe is "A" for example
                {
                    hiddenWord = hiddenWord.Remove(i, 1);
                    hiddenWord = hiddenWord.Insert(i, passMe);
                }
            }

for(int i=0;i您在哪里定义
passMe