简体   繁体   English

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

[英]List[i].Replace in for-loop won't return string

Trying to make hangman (i'm still a newbie) and the program chooses a random word out of a textfile ==> word turned into arrays. 试图制作子手(我仍然是新手),程序从文本文件中选择一个随机单词==>变成数组的单词。 And i have to put it in a label while having the textlabel modified to what's in the letterlist . 而且我必须将其放在标签中,同时将textlabel修改为letterlist中的letterlist Thing is: it doesn't show anything in the label and i can't seem to figure out why. 问题是:它在标签中没有显示任何内容 ,而且我似乎无法弄清楚原因。

So the for-loop is the modifier and when it has modified every string in the list it should return the word with the right letter or "_". 因此,for循环是修饰符,当它修改了列表中的每个字符串时,应返回带有正确字母或“ _”的单词。

At first i tried is by doing: letterlist[i] = Letter or letterlist[i] = "_" , but would happen is if i typed in a right letter it would show only that letter. 起初,我尝试这样做: letterlist[i] = Letterletterlist[i] = "_" ,但是如果我输入正确的字母,它将仅显示该字母。

For example: word = "pen". 例如:word =“ pen”。 If i typed in "p", it resulted in "ppp". 如果我输入“ 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;

New code but it only shows "___" ("_" as many times as the amount of letters as word has): 新代码,但只显示“ ___”(“ _”是单词的字母数量的多少倍):

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;

First of all, note that C# string are immutable, which means letterlist[i].Replace(" ", "_") does not replace spaces with underscores. 首先,请注意C#字符串是不可变的,这意味着letterlist[i].Replace(" ", "_") 不会用下划线替换空格。 It returns a new string in which spaces have been replaced with underscores. 返回一个新的字符串,其中的空格已由下划线替换。
Therefore, you should reassign this result: 因此,您应该重新分配以下结果:

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

Second, Replace(Letter, Letter) won't do much. 其次, Replace(Letter, Letter)不会做太多事情。

Third, in your first for loop, you set every item in letterlist to " " . 第三,在第一个for循环中,将letterlist每个项目都letterlist" "
I don't understand then why you expect (in your second for loop) letterlist[i].Contains("_") to ever be true . 我不明白,然后您为什么期望(在第二个for循环中) letterlist[i].Contains("_") 永远true

Finally, I'll leave here something you might find interesting (especially the use of StringBuilder ): 最后,我将在这里留下您可能会感兴趣的东西(尤其是使用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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM