List [i]。替换为for循环不会返回字符串

时间:2019-01-28 11:21:17

标签: c# string list replace

试图制作子手(我仍然是新手),程序从文本文件中选择一个随机单词==>单词变成数组。而且我必须将其放在标签中,同时将textlabel修改为letterlist中的内容。问题是:它在标签中没有显示任何内容,而且我似乎也找不到原因。

因此for循环是修饰符,当它修改了列表中的每个字符串时,应返回带有正确字母或“ _”的单词。

起初我尝试这样做:letterlist[i] = Letterletterlist[i] = "_",但是会发生的情况是,如果我输入正确的字母,则只会显示该字母。

例如:word =“ pen”。如果我输入“ p”,则结果为“ ppp”。

letterlist = new List<string>();

char[] wordarray = woord.GetWordcharArray(); //word in charArrays
string newwordstring = new string(wordarray);

for (int i = 0; i < wordarray.Length; i++)
{
    letterlist.Add(" "); //adds empty strings in list with the length of the word 
}

/*
 * For-loop for every string in List to check and modify if it's correct or not 
 */
for (int i = 0; i < letterlist.Count; i++)
{
    if (letterlist[i].Contains(Letter) && newwordstring.Contains(Letter)) //right answer: letter[i] = Letter
    {
        letterlist[i].Replace(Letter, Letter);
    }
    else if (letterlist[i].Contains(" ") && newwordstring.Contains(Letter)) //right answer: letter[i] = "" 
    {
        letterlist[i].Replace(" ", Letter);
    }
    else if (letterlist[i].Contains("_") && newwordstring.Contains(Letter)) //right answer: letter[i] = "_"
    {
        letterlist[i].Replace("_", Letter);
    }
    else if (letterlist[i].Contains(" ") && !newwordstring.Contains(Letter)) //wrong answer: letter[i] = "" 
    {
        letterlist[i].Replace(" ", "_");
    }
    else if (letterlist[i].Contains("_") && !newwordstring.Contains(Letter)) //wrong answer: letter[i] = "_"
    {
        letterlist[i].Replace(" ", "_");
    }
}

/*
 * empty += every modified letterlist[i]-string
 */
string empty = "";
foreach (string letter in letterlist)
{
    empty += letter;
}
return empty;

新代码,但仅显示“ ___”(“ _”是单词数量的多少倍):

char[] wordarray = woord.GetWordcharArray(); //word in charArrays
string newwordstring = new string(wordarray); //actual word
string GuessedWord = new string('_', newwordstring.Length);//word that shows in form

bool GuessLetter(char letterguess)
{
   bool guessedright = false;

   StringBuilder builder = new StringBuilder(GuessedWord);

   for(int i = 0; i < GuessedWord.Length; i++)
   {
        if(char.ToLower(wordarray[i]) == Convert.ToChar(Letter))
        {
            builder[i] = wordarray[i];
            guessedright = true;
        }
    }

    GuessedWord = builder.ToString();
    return guessedright;
}

return GuessedWord;

1 个答案:

答案 0 :(得分:2)

首先,请注意C#字符串是不可变的,这意味着letterlist[i].Replace(" ", "_") 不会用下划线替换空格。它返回一个字符串,其中的空格已被下划线替换。
因此,您应该重新分配以下结果:

letterlist[i] = letterlist[i].Replace(" ", "_");

第二,Replace(Letter, Letter)不会做很多事情。

第三,在第一个for循环中,将letterlist中的每个项目都设置为" "
我不明白,为什么您期望(在第二个for循环中)letterlist[i].Contains("_")成为永远成为true

最后,我将在这里留下您可能会感兴趣的内容(尤其是使用StringBuilder):

class Hangman
{
    static void Main()
    {
        Hangman item = new Hangman();
        item.Init();

        Console.WriteLine(item.Guessed); // ____

        item.GuessLetter('t'); // true
        Console.WriteLine(item.Guessed); // T__t

        item.GuessLetter('a'); // false
        Console.WriteLine(item.Guessed); // T__t

        item.GuessLetter('e'); // true
        Console.WriteLine(item.Guessed); // Te_t
    }

    string Word {get;set;}
    string Guessed {get;set;}

    void Init()
    {
        Word = "Test";
        Guessed = new string('_',Word.Length);
    }

    bool GuessLetter(char letter)
    {
        bool guessed = false;

        // use a stringbuilder so you can change any character
        var sb = new StringBuilder(Guessed);

        // for each character of Word, we check if it is the one we claimed
        for(int i=0; i<Word.Length; i++)
        {
            // Let's put both characters to lower case so we can compare them right
            if(Char.ToLower(Word[i]) == Char.ToLower(letter)) // have we found it?
            {
                // Yeah! So we put it in the stringbuilder at the same place
                sb[i] = Word[i];
                guessed = true;
            }
        }

        // reassign the stringbuilder's representation to Guessed
        Guessed = sb.ToString();

        // tell if you guessed right
        return guessed;
    }
}