找出单词中所有相同的字母 [英] Find every same letters in a word

查看:72
本文介绍了找出单词中所有相同的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Hangman 项目,该项目要求我将字符-"更改为a 到 z".由于我正在学习如何使用 C# 进行编码,因此我不知道该怎么做.我显然需要使用位置,因为单词作为重复字母的情况(例如:C oo kies)

I'm working on a Hangman project that requires me to change characters "-" to "a to z". Since I'm learning how to code with C#, I have no clue how to do it. I obviously need to use position because of the case where the word as duplicated letters (EX.: C oo kies)

这是我开发的代码,它让我的东西崩溃,而且显然不完整.

Here's the code I developed, it makes my thing crashes and it's obviously incomplete.

    private void chkA_Checked(object sender, RoutedEventArgs e)
    {
        if (motRechercher.Contains("a"))
        {
            int indexDemotRechercher = motRechercher.IndexOf("a");
            int k = indexDemotRechercher;
            var StringBuilderOP = new StringBuilder(motRechercher);
            StringBuilderOP.Remove(indexDemotRechercher, indexDemotRechercher);
            StringBuilderOP.Insert(k, "A");
        }}

motRechercher 是一个字符串,我可以在任何地方使用它,我从 27 个单词的列表中随机选择.如果这个麻烦,它是一个复选框,我写文本的地方是一个文本框(称为txtMot).随意使用其他变量,我会重新适应我自己的理解.我只是想要一些解释/例子来帮助我的学习体验.

motRechercher is a STRING that I can use everywhere that I randomly pick from a list of 27 words. If this bother, it's a check-box and where I write the text is a Text-box(called txtMot). Feel free to use other variables, I'll re-adapt after for my own comprehension. I would just like some explanation/examples to help my learning experience.

如果您真的觉得这可以帮助您理解,这里是随机生成器的代码[它有效]:

Here is the code of the randomiser if you really feel like this can help you understand[It works] :

        private void btnDemarrer_Click(object sender, RoutedEventArgs e)
    {
     Random rdn = new Random();
     int nbreAleatoire = rdn.Next(0, 27); 
     motRechercher = lesMots[nbreAleatoire];
            if (motRechercher.Length > 0)
            {
            String str = new String('-', motRechercher.Length);
            txtMot.Text = str;
            }
    }

问题:我如何制作一个检测重复的东西并将-"更改为a-z"?

QUESTION : How do I make a thing that detects duplicate and that will change the "-" to "a-z"?

提出问题,如果您认为不清楚,我会尽力回答.

Ask questions and I'll try to answer them if you think it's unclear.

推荐答案

这是一个快速示例...我有两个字符串...一个用于用户看不到的隐藏单词,另一个用于显示的单词,使用-"甚至_"作为实际字符的占位符.

Here is a quick sample... I have two strings... one for the hidden word the user does NOT see, and another for the one presented, using "-" or even "_" as place-holders for the actual characters.

我有一个简单的函数IsThereA",它需要一个字母来猜测单词中的一个字母.然后我只需调用所有字母,包括一些随机字母.该函数返回布尔值,因此您可以在每次失败时绘制刽子手.

I have a simple function "IsThereA" which expects a single letter as to the guess of a letter in the word. I then just call for all the letters including a few random ones. The function returns boolean so you can draw the hangman as each failure occurs.

在IsThereA"方法中,我一次查找一个字符来寻找猜测的字母.如果找到,我会使用子字符串而不是-"来替换它.因此,一旦更新,您就可以根据需要使用WordUserCanSee"属性.

In the "IsThereA" method, I am looking one character at a time for the guessed letter. If found, I replace it by using substring instead of the "-". So once updated, you can use the "WordUserCanSee" property however you need to.

此版本不区分大小写,但您可以根据需要进行调整.

This version doesn't do case-sensitive, but you can adjust as needed.

public class Hangman
{
    string HangmanWord = "cookies";
    string WordUserCanSee = "-------";

    public Hangman()
    {
        IsThereA("o");
        IsThereA("f");
        IsThereA("k");
        IsThereA("w");
        IsThereA("i");
        IsThereA("c");
        IsThereA("s");
        IsThereA("e");
    }


    public bool IsThereA(string guessLetter)
    {
        bool anyMatch = false;
        for (int i = 0; i < HangmanWord.Length; i++)
        {
            if (HangmanWord.Substring(i, 1).Equals(guessLetter))
            {
                anyMatch = true;
                WordUserCanSee = WordUserCanSee.Substring(0, i) + guessLetter + WordUserCanSee.Substring(i + 1);
            }
        }

        return anyMatch;
    }
}

这篇关于找出单词中所有相同的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆