【发布时间】:2014-05-20 14:15:48
【问题描述】:
所以我在这里得到了以下代码...我必须提供 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();
【问题讨论】:
-
获取字母的索引(如果存在)并用该索引上的字母替换'_'
-
请将整个 switch 语句替换为:
word = words[randomword - 1]; tries = word.Length; -
@musefan 甚至只是将随机词更改为
rand.Next(0, 4)并删除 -1。 -
@Gray:确实!没有什么让可怜的小伙子感到困惑