代码以我想要的方式工作。我在制作一个数组来替换Console.WriteLine中的*时遇到了麻烦(“这个词是*******。”); ....阵列必须在正确的位置显示正确的字母,我绝望地失去了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int correctcounter = 0;
int wrongcounter = 0;
char l1 = 'j';
char l2 = 'o';
char l3 = 'h';
char l4 = 'n';
char l5 = 's';
char[] correctletters = { 'j', 'o', 'h', 'n', 's', 'o', 'n' };
char[] guessedletters = new char[10];
Console.WriteLine("Welcome to the Hangman Game!");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("You have 10 tries to guess the word right.");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("The word is *******.");
Console.WriteLine();
Console.WriteLine("Guessed letters: [{0}]", string.Join(",", guessedletters));
Console.WriteLine();
Console.WriteLine("Your total wrong guesses:{0}.", wrongcounter);
Console.WriteLine();
Console.WriteLine("Please enter a letter");
Console.WriteLine();
guessedletters[i] = Convert.ToChar(Console.ReadLine());
if (guessedletters[i] == l1 || guessedletters[i] == l2 || guessedletters[i] == l3 || guessedletters[i] == l4 || guessedletters[i] == l5)
{
Console.WriteLine();
Console.WriteLine("You guessed correctly!");
correctletters[i] = guessedletters[i];
correctcounter++;
}
else
{
Console.WriteLine();
Console.WriteLine("You guessed incorrectly");
wrongcounter++;
}
if (correctcounter == 5)
{
Console.WriteLine();
Console.WriteLine("You guessed the word, [{0}]. You WIN!", string.Join("",correctletters));
break;
}
if (wrongcounter == 10)
{
Console.WriteLine();
Console.WriteLine("You LOSE! The word was [{0}]. You LOSE!", string.Join("", correctletters));
break;
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Please hit enter to end the program");
Console.ReadLine();
}
}
}
答案 0 :(得分:3)
好的,让我们尝试用您已经知道的结构慢慢地做到这一点。
因此,为了实现我们的目标,我们将使用另一个数组,该数组将保留向用户显示的单词。我们称之为“WordToReveal&#39;:
”char[] wordToReveal = {'*', '*', '*', '*', '*', '*', '*'};
当用户找到正确的字母时,我们会用正确的字母填写。
如果我们转到第一个if
,我们可以看到您已经尝试使用guessedLetters更改correctLetters:
correctletters[i] = guessedletters[i];
但是,这段代码的作用是错误的。您正在使用索引i
(转向#)将正确的字母更改为用户猜到的字母。因此,如果我们在第3个回合,用户输入&#39; j,那么你最终会得到正确的单词,如下所示:joh j 儿子
而不是这样,我们将循环播放您当前的正确信函&#39;并更改&#34;字以显示&#34;显示真实的字母。要循环播放,我们使用其他for
循环,使用名为j
的变量(因为i
已被占用!)。该变量将从0到数组末尾。
在该循环中,我们将检查用户是否猜到了索引j处的字母。如果是这种情况,我们会用正确的字母覆盖*
:
for (int j = 0; j < correctletters.Length; j++)
{
if (guessedletters.Contains(correctletters[j]))
{
wordToReveal[j] = correctletters[j];
}
}
最后,我们需要显示这个词。为此,我们会将您的字符串"The word is *******."
替换为我们的新字词以显示:
Console.WriteLine("The word is " + string.Concat(wordToReveal) + ".");
顺便说一下,string.Concat
与您使用的string.Join
完全相同。区别在于您不必将空字符串指定为分隔符,它将数组自身连接在一起。
最终代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int correctcounter = 0;
int wrongcounter = 0;
char l1 = 'j';
char l2 = 'o';
char l3 = 'h';
char l4 = 'n';
char l5 = 's';
char[] correctletters = { 'j', 'o', 'h', 'n', 's', 'o', 'n' };
char[] wordToReveal = {'*', '*', '*', '*', '*', '*', '*'};
char[] guessedletters = new char[10];
Console.WriteLine("Welcome to the Hangman Game!");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("You have 10 tries to guess the word right.");
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("The word is " + string.Concat(wordToReveal) + ".");
Console.WriteLine();
Console.WriteLine("Guessed letters: [{0}]", string.Join(",", guessedletters));
Console.WriteLine();
Console.WriteLine("Your total wrong guesses:{0}.", wrongcounter);
Console.WriteLine();
Console.WriteLine("Please enter a letter");
Console.WriteLine();
guessedletters[i] = Convert.ToChar(Console.ReadLine());
if (guessedletters[i] == l1 || guessedletters[i] == l2 || guessedletters[i] == l3 || guessedletters[i] == l4 || guessedletters[i] == l5)
{
Console.WriteLine();
Console.WriteLine("You guessed correctly!");
for (int j = 0; j < correctletters.Length; j++)
{
if (guessedletters.Contains(correctletters[j]))
{
wordToReveal[j] = correctletters[j];
}
}
correctcounter++;
}
else
{
Console.WriteLine();
Console.WriteLine("You guessed incorrectly");
wrongcounter++;
}
if (correctcounter == 5)
{
Console.WriteLine();
Console.WriteLine("You guessed the word, [{0}]. You WIN!", string.Concat(correctletters));
break;
}
if (wrongcounter == 10)
{
Console.WriteLine();
Console.WriteLine("You LOSE! The word was [{0}]. You LOSE!", string.Concat(correctletters));
break;
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Please hit enter to end the program");
Console.ReadLine();
}
}
}
你可以看到我们用另一个声明了新数组,更改了Console.Write
并在第一个if中添加了新的循环。
还有很多其他方法可以实现它,但我希望这对你来说已经足够清楚了。
答案 1 :(得分:1)
这是一种方式:
var secretWord = "shmurple"; // the secret word
var mask = new String('*', secretWord.Length); // the mask
在透露信件时:
var sb = new StringBuilder(mask); // to be able to alter at index
sb[2] = secretWord[2];
mask = sb.ToString();
示例:
var secretWord = "shmurple";
var mask = new String('*', secretWord.Length);
var sb = new StringBuilder(mask);
sb[2] = secretWord[2];
mask = sb.ToString();
Console.WriteLine(String.Format("The word is {0}.", mask)); // **m*****
这是一种方式......但还有更多。我给了这个一分钟的考虑,所以你可能会找到一种更有效的方式。
或者,secretWord
可以是char
数组,因此您无需担心整个StringBuilder
业务。其他一切都以同样的方式运作。
修改强>
我看到你的新代码....只需创建一个掩码数组(与密码相同的长度),当你显示一个字母时,将其替换为正确数组中的char
:
char[] secret = { 'j', 'o', 'h', 'n', 's', 'o', 'n' };
char[] solution = Enumerable.Repeat('*', secret.Length).ToArray(); // mask
solution[2] = secret[2]; // example
Console.WriteLine(solution); // **h****
这样,你的所有索引都匹配,你的生活也变得更加简单。如果您需要显示所有匹配项,只需使用循环。