Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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

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

C#循环问题中的刽子手

C#循环问题中的刽子手,c#,loops,C#,Loops,所以我得到了下面的代码。。。我必须提供5个用户提供的单词,在5个单词上随机分配,并给出其中任何一个单词进行猜测,trys=word length+2。我遇到的主要问题是循环整个检查以填写第二、第三个猜测等。第一个猜测很好。我将如何循环并保留猜测正确的字符,同时仍保留未猜测为“u”字符的字符 示例-单词是“天堂”-用户输入“e”-生成-\e\ue\uuu没有空格。 尝试次数将等于6次(字长)+2=8次尝试 int tries = 0; Random rand = new Random(); int

所以我得到了下面的代码。。。我必须提供5个用户提供的单词,在5个单词上随机分配,并给出其中任何一个单词进行猜测,
trys=word length+2
。我遇到的主要问题是循环整个检查以填写第二、第三个猜测等。第一个猜测很好。我将如何循环并保留猜测正确的字符,同时仍保留未猜测为“u”字符的字符

示例-
单词是“天堂”-用户输入“e”-生成-\e\ue\uuu
没有空格。 尝试次数将等于6次(字长)+2=8次尝试

int tries = 0;
Random rand = new Random();
int randomword = rand.Next(1, 5);
string word = "";

Console.WriteLine("Please Enter 5 Words into the System");
Console.WriteLine();

for (int i = 0; i < 5; i++)
{
    words.Add(Console.ReadLine());
    Console.Clear();
}

Console.WriteLine("For Display Purposes. Here are Your 5 Words");
Console.WriteLine("===========================================");
Console.WriteLine();
foreach (var item in words)
{
    Console.WriteLine(item);
}

Console.WriteLine();
Console.WriteLine("Now Guess The word given Below");
Console.WriteLine();

switch (randomword)
{
    case 1:
        //Gets the List index 0 - 1st word in the list
        word = words[0];
        tries = word.Length;
        break;
    case 2:
        word = words[1];
        tries = word.Length;
        break;
    case 3:
        word = words[2];
        tries = word.Length;
        break;
    case 4:
        word = words[3];
        tries = word.Length;
        break;
    case 5:
        word = words[4];
        tries = word.Length;
        break;
    default:
        break;
}
//Default + addition to the Length
tries += 2;

Console.WriteLine();
Console.WriteLine("You Have {0} + tries",tries );
//Works for the 1st Guess
do
{
    char guess = char.Parse(Console.ReadLine());
    if (word.Contains(guess))
    {
        foreach (char item in word)
        {
            if (item == guess)
            {
                Console.Write(item);
            }
            else
            {
                Console.Write("_");
            }
        }
    }
    Console.WriteLine();
} 
//If my word contains A "_" i will keep looping
while (word.Contains("_"));

Console.ReadKey();
int=0;
Random rand=新的Random();
int randomword=rand.Next(1,5);
字串=”;
Console.WriteLine(“请在系统中输入5个单词”);
Console.WriteLine();
对于(int i=0;i<5;i++)
{
words.Add(Console.ReadLine());
Console.Clear();
}
Console.WriteLine(“用于显示目的,这里是您的5个单词”);
Console.WriteLine(“==========================================================”);
Console.WriteLine();
foreach(var项目大写)
{
控制台写入线(项目);
}
Console.WriteLine();
WriteLine(“现在猜猜下面给出的单词”);
Console.WriteLine();
开关(随机字)
{
案例1:
//获取列表索引0-列表中的第一个单词
单词=单词[0];
尝试=单词长度;
打破
案例2:
单词=单词[1];
尝试=单词长度;
打破
案例3:
单词=单词[2];
尝试=单词长度;
打破
案例4:
单词=单词[3];
尝试=单词长度;
打破
案例5:
单词=单词[4];
尝试=单词长度;
打破
违约:
打破
}
//默认+长度的增加
尝试次数+=2;
Console.WriteLine();
WriteLine(“您有{0}+次尝试”,尝试);
//一猜就知道了
做
{
char guess=char.Parse(Console.ReadLine());
if(单词包含(猜测))
{
foreach(word中的字符项)
{
如果(项==猜测)
{
控制台。写入(项);
}
其他的
{
控制台。写(“”);
}
}
}
Console.WriteLine();
} 
//如果我的单词包含一个“u”,我将继续循环
while(word.Contains(“”);
Console.ReadKey();

您需要存储猜测结果并检查循环的猜测结果(将
do while
循环更改为如下所示):

string resultword=word;
做
{
char guess=char.Parse(Console.ReadLine());
if(单词包含(猜测))
{
resultword=resultword.Replace(猜测为“”);
for(int i=0;i
那么:

static void Main(string[] args)
{

    string[] words = new string[] { "Apple", "Banana", "Pear", "Pineapple", "Melon"};
    Random random = new Random();

    string wordToGuess = words[random.Next(5)].ToLower();
    char[] currentLetters = new char[wordToGuess.Length];
    for (int i = 0; i < currentLetters.Length; i++) currentLetters[i] = '_';
    int numTries = currentLetters.Length + 1;
    bool hasWon = false;
    do
    {
        string input = Console.ReadLine().ToLower();
        if (input.Length == 1) //guess a letter
        {
            char inputChar = input[0];
            for (int i = 0; i < currentLetters.Length; i++)
            {
                if (wordToGuess[i] == inputChar)
                {
                    currentLetters[i] = inputChar;
                }
            }
            if (!currentLetters.Contains('_'))
            {
                hasWon = true;
            }
            else
            {
                Console.WriteLine(new string(currentLetters));
            }
        }
        else
        {
            if (input == wordToGuess)
            {
                hasWon = true;
            }
            else
            {
                Console.WriteLine("Incorrect!");
                Console.WriteLine(new string(currentLetters));
            }
        }

        numTries--;

    } while (new string(currentLetters) != wordToGuess && numTries > 0 && !hasWon);

    if (hasWon)
    {
        Console.WriteLine("Congratulations, you guessed correctly.");
    }
    else
    {
        Console.WriteLine("Too bad! Out of tries");
    }
}
static void Main(字符串[]args)
{
字符串[]单词=新字符串[]{“苹果”、“香蕉”、“梨”、“菠萝”、“瓜”};
随机=新随机();
string wordToGuess=words[random.Next(5)].ToLower();
char[]currentLetters=新字符[wordToGuess.Length];
对于(int i=0;i0&&!haswen);
如果(哈斯旺)
{
WriteLine(“祝贺你,你猜对了。”);
}
其他的
{
Console.WriteLine(“太糟糕了!没有尝试”);
}
}

您的主要问题是,您只跟踪当前猜测,而不是之前的所有猜测。您可以使用
HashSet
来跟踪以前的猜测。因此,在
do
循环之前定义一个变量
HashSet guessedlets
,然后在解析“guess”do
guessedlets.Add(guess)
之后,将其作为循环中的第二行。然后将
if(item==guess)
替换为
if(guessedLetters.Contains(item))
。维奥拉,只有三行代码更改


最后,退出条件可以是while(word.Any(c=>!guessedChars.Contains(c))&&--trys!=0)

如何,使用您现有的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<string> words = new List<string>();
            int tries = 0;
            Random rand = new Random();
            var currentLetters = new List<char>();
            int randomword = rand.Next(1, 5);
            string word = "";

            Console.WriteLine("Please Enter 5 Words into the System");
            Console.WriteLine();

            for (int i = 0; i < 5; i++)
            {
                words.Add(Console.ReadLine());
                Console.Clear();
            }

            Console.WriteLine("For Display Purposes. Here are Your 5 Words");
            Console.WriteLine("===========================================");
            Console.WriteLine();
            foreach (var item in words)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            Console.WriteLine("Now Guess The word given Below");
            Console.WriteLine();

            switch (randomword)
            {
                case 1:
                    //Gets the List index 0 - 1st word in the list
                    word = words[0];
                    tries = word.Length;
                    break;
                case 2:
                    word = words[1];
                    tries = word.Length;
                    break;
                case 3:
                    word = words[2];
                    tries = word.Length;
                    break;
                case 4:
                    word = words[3];
                    tries = word.Length;
                    break;
                case 5:
                    word = words[4];
                    tries = word.Length;
                    break;
                default:
                    break;
            }
            //Default + addition to the Length
            tries += 2;

            Console.WriteLine();
            Console.WriteLine("You Have {0} + tries", tries);
            //Works for the 1st Guess
            do
            {
                char guess = char.Parse(Console.ReadLine());
                if (!currentLetters.Contains(guess))
                {
                    currentLetters.Add(guess);
                    foreach (var l in word.ToCharArray().Intersect(currentLetters).ToArray())
                    {
                        word = word.Replace(l, '_');
                    }
                }
                Console.WriteLine(word);
            } //If my word contains A "_" i will keep looping
            while (word.Contains("_"));

        Console.ReadKey();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序3
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
列表单词=新列表();
int=0;
Random rand=新的Random();
var currentLetters=新列表();
int randomword=rand.Next(1,5);
字串=”;
Console.WriteLine(“请在系统中输入5个单词”);
Console.WriteLine();
对于(int i=0;i<5;i++)
{
words.Add(Console.ReadLine());
Console.Clear();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            List<string> words = new List<string>();
            int tries = 0;
            Random rand = new Random();
            var currentLetters = new List<char>();
            int randomword = rand.Next(1, 5);
            string word = "";

            Console.WriteLine("Please Enter 5 Words into the System");
            Console.WriteLine();

            for (int i = 0; i < 5; i++)
            {
                words.Add(Console.ReadLine());
                Console.Clear();
            }

            Console.WriteLine("For Display Purposes. Here are Your 5 Words");
            Console.WriteLine("===========================================");
            Console.WriteLine();
            foreach (var item in words)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            Console.WriteLine("Now Guess The word given Below");
            Console.WriteLine();

            switch (randomword)
            {
                case 1:
                    //Gets the List index 0 - 1st word in the list
                    word = words[0];
                    tries = word.Length;
                    break;
                case 2:
                    word = words[1];
                    tries = word.Length;
                    break;
                case 3:
                    word = words[2];
                    tries = word.Length;
                    break;
                case 4:
                    word = words[3];
                    tries = word.Length;
                    break;
                case 5:
                    word = words[4];
                    tries = word.Length;
                    break;
                default:
                    break;
            }
            //Default + addition to the Length
            tries += 2;

            Console.WriteLine();
            Console.WriteLine("You Have {0} + tries", tries);
            //Works for the 1st Guess
            do
            {
                char guess = char.Parse(Console.ReadLine());
                if (!currentLetters.Contains(guess))
                {
                    currentLetters.Add(guess);
                    foreach (var l in word.ToCharArray().Intersect(currentLetters).ToArray())
                    {
                        word = word.Replace(l, '_');
                    }
                }
                Console.WriteLine(word);
            } //If my word contains A "_" i will keep looping
            while (word.Contains("_"));

        Console.ReadKey();
    }
}
    string displayword = String.Copy(word);
    for (int j = 0; j < displayword.length; j++) displayword[j]='_';
    do {
        // Display the word so far.
        Console.WriteLine("Word is {0}", displayword);

        // Get a guess from the user
        char guess = char.Parse(Console.ReadLine());
        if (word.Contains(guess)) {
            for (j=0; j<word.length; j++) {
              if (word[j] == guess) displayword[j]=guess;
            }
        } else {
          // Decrease the tries.
          tries--;
        }
    } while (displayword.Contains("_") && (tries > 0));
        List<string> words = new List<string>();
        int tries = 0;
        Random rand = new Random();
        int randomword = rand.Next(1, 5);
        string word = "";

        Console.WriteLine("Please Enter 5 Words into the System");
        Console.WriteLine();

        for (int i = 0; i < 5; i++)
        {
            words.Add(Console.ReadLine());
            Console.Clear();
        }

        Console.WriteLine("For Display Purposes. Here are Your 5 Words");
        Console.WriteLine("===========================================");
        Console.WriteLine();
        foreach (var item in words)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine();
        Console.WriteLine("Now Guess The word given Below");
        Console.WriteLine();

        switch (randomword)
        {
            case 1:
                //Gets the List index 0 - 1st word in the list
                word = words[0];
                tries = word.Length;
                break;
            case 2:
                word = words[1];
                tries = word.Length;
                break;
            case 3:
                word = words[2];
                tries = word.Length;
                break;
            case 4:
                word = words[3];
                tries = word.Length;
                break;
            case 5:
                word = words[4];
                tries = word.Length;
                break;
            default:
                break;
        }
        //Default + addition to the Length
        tries += 2;

        Console.WriteLine();
        Console.WriteLine("You Have {0} + tries", tries);

        List<char> guesses = new List<char>();
        string guessedWord = "";

        for(int i=0;i<word.Length;i++)
        {
            guessedWord += "_";
        }

        //Works for the 1st Guess
        do
        {
            char guess = char.Parse(Console.ReadLine());

            if (word.Contains(guess))
            {
                guesses.Add(guess);
            }

            foreach (char storedGuess in guesses)
            {
                if(word.Contains(storedGuess))
                {
                    int index = word.IndexOf(storedGuess);
                    while(index > -1)
                    {
                        StringBuilder sb = new StringBuilder(guessedWord);
                        sb[index] = storedGuess;
                        guessedWord = sb.ToString();

                        index = word.IndexOf(storedGuess, index+1);
                    }
                }
            }

            Console.WriteLine(guessedWord);
        }
        while (guessedWord.Contains("_"));

        Console.ReadKey();
        var result = new string('_', word.Length).ToCharArray();
        do
        {
            char guess = char.Parse(Console.ReadLine());
            for (var i = 0; i < word.Length; ++i)
            {
                if(word[i] == guess)
                {
                    result[i] = guess;
                }
            }
            Console.WriteLine(new string(result));
        }
        //If my word contains A "_" i will keep looping
        while (result.Contains('_') && --tries != 0);