Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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# 我如何确定在hangman c游戏中用户猜测错误的概率#_C#_Console - Fatal编程技术网

C# 我如何确定在hangman c游戏中用户猜测错误的概率#

C# 我如何确定在hangman c游戏中用户猜测错误的概率#,c#,console,C#,Console,我正在做一个hangman游戏,我想知道如果用户猜到单词中没有的字母,如何显示一条信息,并将数字增加1。现在发生的情况是,当用户猜测单词中没有的字母时,会为单词中的每个字母显示一条消息,并为单词中的每个字母增加一个数字 我试图让它,当用户作出错误的猜测,它只显示该消息一次,只增加了一个数字。下面是我的代码,用于确定用户何时猜错 for (int index = 0; index < charArray.Length; index++) {

我正在做一个hangman游戏,我想知道如果用户猜到单词中没有的字母,如何显示一条信息,并将数字增加1。现在发生的情况是,当用户猜测单词中没有的字母时,会为单词中的每个字母显示一条消息,并为单词中的每个字母增加一个数字

我试图让它,当用户作出错误的猜测,它只显示该消息一次,只增加了一个数字。下面是我的代码,用于确定用户何时猜错

for (int index = 0; index < charArray.Length; index++)
            {
                if (charArray[index] == userGuess && userGuess != lettersUsed[index])
                {
                    found[index] = userGuess;
                    lettersUsed[index] = userGuess;
                }
                else if (lettersUsed[index] == userGuess)
                {
                    Console.WriteLine($"{userGuess} is already in the word");
                }
                else if (charArray[index] != userGuess)
                {
                    Console.WriteLine($"{userGuess} is not in the word");
                    guesses++;
                }
            }
for(int index=0;index

任何建议都会很棒

我认为charArray是用来存储用户试图猜测的原始单词的。如果我是你,我会查看存储用户找到的字符的列表

var found=newlist();
...
if(charArray.Any(c=>c==userGuess))//如果char数组中的任何字符等于猜测值
{
if(find.Any(c=>c==userGuess))
{
WriteLine($“{userGuess}已在word中);
}
其他的
{
添加(userGuess);
}
}
其他的
{
WriteLine($“{userGuess}不在单词中”);
猜测++;
}

顺便说一下,Any是一个Linq扩展方法,它检查集合中的任何元素是否与作为参数输入的谓词匹配。为此,必须使用类似javascript的箭头函数语法。

如果字母已经在单词中。打印“x已在单词中”消息charArray.Length()的次数。使用bool确定是否找到了该信件,然后仅打印一次邮件

class HangmanGame
{
    char[] charArray = {'h', 'a', 'n', 'g', 'm', 'a', 'n'};
    char[] found = {'*', '*', '*', '*', '*', '*', '*'};
    int guesses = 0;

    public void TestLetter(char userGuess)
    {
        bool foundLetter = false, alreadyInWord = false;
        for (int index = 0; index < charArray.Length; index++)
        {
            if(userGuess == found[index])
            {
                alreadyInWord = true;
                break;
            }
            else if(userGuess == charArray[index])
            {
                found[index] = charArray[index];
                foundLetter = true;
            }
        }
        if(alreadyInWord)
        {
            String s = "The letter " + userGuess + " was already in the word: " + new string(found) + ".";
            Console.WriteLine(s);
        }
        else if(foundLetter)
        {
            String s = "You guessed correctly: " + new string(found) + ".";
            Console.WriteLine(s);
        }
        else
        {
            guesses++;
        }
    }
}
class-HangmanGame
{
char[]charArray={'h','a','n','g','m','a','n'};
char[]found={'*','*','*','*','*','*','*','*','*'};
整数猜测=0;
公共void TestLetter(char userGuess)
{
bool foundLetter=false,alreadyInWord=false;
for(int index=0;index
使用的字母和找到的字母有什么区别?它们似乎存储相同的数据。这一点很好!这有点毫无意义。我应该使用find,但我会确定他们是否猜到单词中没有的字母?