我目前正在开展一个小项目,我正在创建一个作为控制台应用程序的刽子手游戏。目前,我遇到了问题,因为我想为要猜的单词创建一个数组。这个词不是随机的,而且总是一样的。在这种情况下,它是"米勒"。我想为" miller"创建一个数组。并有一个循环来显示单词字符数组中的每个字母。当猜到正确的字母时,我想显示正确的字母,如果没有显示特殊字符" *"取而代之。所以,例如,这个词是"米勒"并将首先显示为" ******"。当用户正确猜出一个字母时,一个特殊字符将成为正确猜到的字母。因此,用户猜测" i" ...显示的单词将是" * i ****"。说他猜测" l"接下来......显示的单词将是" * ill **"。我该怎么做呢?到目前为止,这是我的代码...我已经创建了已经尝试为此创建数组。很抱歉,如果这看似微不足道,但我是编程新手,只需要一些指导和帮助。
TLDR; 我需要这个代码的帮助。我正在尝试制作一个包含' miller'当用户猜出一个正确的字母更改特殊字符的显示时,我希望能够调用该数组" *"不管他猜错的是什么字母。我被告知关于for循环是最好的途径,但我有点迷失。帮助
static void Main(string[] args)
{
char[] guessed = new char[26];
char[] word = "miller".ToCharArray();
char guess;
int score = 0, index = 0;
Console.WriteLine("******");
for (int i = 0; i < 10; i++)
{
Console.Write("Please enter a letter to guess: ");
guess = char.Parse(Console.ReadLine());
if (guess == l1 || guess == l2 || guess == l3 || guess == l4 || guess == l5 || guess == l6)
{
Console.WriteLine("Your guess is correct.");
guessed[index] = guess;
index++;
}
else
{
Console.WriteLine("Your guess is incorrect.");
score++;
}
}
Console.WriteLine("Your score is " + score);
Console.ReadLine();
}
}
}
答案 0 :(得分:1)
我不知道这是不是你想要的,也许你下次应该更具体......但我试图为你的问题创建一个解决方案。
static void Main(string[] args)
{
char[] guessed = new char[26];
char[] testword = "******".ToCharArray();
char[] word = "miller".ToCharArray();
char[] copy = word;
char guess;
int score = 0, index = 0;
Console.WriteLine(testword);
for (int i = 0; i < 10; i++)
{
Console.Write("Please enter a letter to guess: ");
guess = char.Parse(Console.ReadLine());
bool right = false;
for (int j = 0; j < copy.Length; j++)
{
if (copy[j] == guess)
{
Console.WriteLine("Your guess is correct.");
testword[j] = guess;
guessed[index] = guess;
index++;
right = true;
}
}
if (right != true)
{
Console.WriteLine("Your guess is incorrect.");
score++;
}
else
{
right = false;
}
Console.WriteLine(testword);
}
Console.WriteLine("Your score is " + score);
Console.ReadLine();
}
答案 1 :(得分:0)
在您的文件中加入using System.Linq;
,这是您的for循环。
for (int 1 = 0; i < 10; i++)
{
Console.Write("Please enter a letter to guess: ");
char guess = char.Parse(Console.ReadLine());
if(word.Contains(guess))//checks if the word is is the array word
{
//the guess was correct.
Console.WriteLine("Your guess is correct.");
guessed[index] = guess;
index++;
}
else
{
//the guess was wrong.
console.WriteLine("Your guess is incorrect.");
....
}
}
希望这有帮助。
答案 2 :(得分:0)
看起来你离那儿很近。我不确定的两件事是if语句“guess == l1 || guess == l2”等等......这可以通过以下方式处理:
if(word.Contains(guess))
其次,你需要另一个for循环来完成正确猜测的字母并放置它们。
for循环填写字母而不是星号: 把它放在for(int i = 10 ...)
的开头for (int j = 0; j < word.Length; j++)
{
if (guessed.Contains(word[j]))
{
Console.Write(word[j]);
}
else
{
Console.Write("*");
}
}