【发布时间】:2014-01-24 14:18:06
【问题描述】:
这是我的刽子手代码,它几乎可以显示猜测的字母。它只显示最近猜测的字母,但我希望它从停止的内容继续。例如,如果一个人猜“A”,然后猜“L”,那么猜字母是 A,L。 我尝试在“猜测字母是”之后使用 for 循环,但它给了我输出 “一个,一个,一个,一个,一个”。我应该怎么做才能解决这个问题?
class Hangman
{
public string[] words = new string[5] { "ARRAY", "OBJECT", "CLASS", "LOOP", "HUMBER" };
public string[] torture = new string[6] { "left arm", "right arm", "left leg", "right leg", "body", "head" };
public char[] guessed = new char[26];
int i;
public void randomizedWord()
{
Random random = new Random();
int index = random.Next(0, 5);
char[] hidden = new char[words[index].Length];
string word = words[index];
Console.WriteLine(words[index]);
Console.Write("The word is: ");
for (i = 0; i < hidden.Length; i++)
{
Console.Write('-');
hidden[i] = '-';
}
Console.WriteLine();
int lives = 6;
do
{
Console.WriteLine("Guess a letter: ");
char userinput = Console.ReadLine().ToCharArray()[0];
index++;
guessed[index] = userinput;
Console.WriteLine("Guessed letters are: " + guessed[index]);
bool foundLetter = false;
for (int i = 0; i < hidden.Length; i++)
{
if (word[i] == userinput)
{
hidden[i] = userinput;
foundLetter = true;
Console.WriteLine("You guessed right!");
}
}
for (int x = 0; x < hidden.Length; x++)
{
Console.Write(hidden[x]);
}
if (!foundLetter)
{
Console.WriteLine(" That is not a correct letter");
lives--;
if (lives == 5)
{
Console.WriteLine("You lost a " + torture[0]);
}
else if (lives == 4)
{
Console.WriteLine("You lost the " + torture[1]);
}
else if (lives == 3)
{
Console.WriteLine("You lost your " + torture[2]);
}
else if (lives == 2)
{
Console.WriteLine("You lost the " + torture[3]);
}
else if (lives == 1)
{
Console.WriteLine("You lost your " + torture[4]);
}
else
{
Console.WriteLine("You lost your " + torture[5]);
Console.WriteLine("You lose!");
break;
}
}
bool founddash = false;
for (int y = 0; y < hidden.Length; y++)
{
if (hidden[y] == '-')
{
founddash = true;
}
}
if (!founddash)
{
Console.WriteLine(" You Win! ");
break;
}
Console.WriteLine();
} while (lives != 0);
}
【问题讨论】:
-
你尝试的循环是什么?
-
@lc。 for (int i = 0; i
-
您的问题在于Debugging in Visual Studio。学习如何使用断点来找出你的程序正在做什么以及为什么需要编写任何重要的代码。
-
各种选项,您可以将字母存储在 char[] 或 list
中(存储错过的字母会更干净,因为没有错过的是正确的),或者更简单地制作一个字符串并且每次用户错过像 yourstring += "-" + letter; 这样的 concat