所以我在这里得到了以下代码......我必须提供 5 个用户提供的单词,在 5 个上随机化并给出其中任何一个以供猜测,tries = word length + 2
. 我遇到的主要问题是循环整个检查以填写第二个、第三个猜测等。第一个猜测很好。我将如何循环并保持正确猜测的字符,同时仍将未猜测的字符保留为“_”字符。
示例 -Word was "Heaven" - User Enters "e" - Produces - _ e _ _ e _
没有空格。然后尝试将等于 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();