C# StringBuilder在隐藏消息中显示空格
因此,本质上我希望displayToPlayer变量自动添加空格,但我不太确定如何实现这一点 我怎样才能让空格自动添加到隐藏消息中,而不使用回合或将其添加到猜测的字母计数中 当前结果: 预期结果:C# StringBuilder在隐藏消息中显示空格,c#,console-application,C#,Console Application,因此,本质上我希望displayToPlayer变量自动添加空格,但我不太确定如何实现这一点 我怎样才能让空格自动添加到隐藏消息中,而不使用回合或将其添加到猜测的字母计数中 当前结果: 预期结果: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace guessingGame { class Program { static
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace guessingGame
{
class Program
{
static void Main(string[] args)
{
string[] quoteList = {
"NEVER GUNNA LET YOU DOWN",
/*"NEVER GUNNA RUN AROUND",
"THIS IS A TEST"*/
}; // the list of quotes that need to be guessed by the player
Random random = new Random((int)DateTime.Now.Ticks);
string quoteToGuess = quoteList[random.Next(0, quoteList.Length)];
string quoteToGuessUppercase = quoteToGuess.ToUpper();
StringBuilder displayToPlayer = new StringBuilder(quoteToGuess.Length);
for (int i = 0; i < quoteToGuess.Length; i++)
{
displayToPlayer.Append('*');
}
List<char> correctGuesses = new List<char>();
List<char> incorrectGuesses = new List<char>();
int quoteToGuessLength = quoteToGuess.Count(characters => !Char.IsWhiteSpace(characters));
int turnsLeft = quoteToGuess.Distinct().Count(characters => !Char.IsWhiteSpace(characters)) + 3;
bool quoteGuessed = false;
int lettersRevealed = 0;
string userInput;
char userGuess;
Console.WriteLine("Can you work out the quote? (you have {0} chances)", turnsLeft);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(displayToPlayer.ToString());
Console.WriteLine();
while (!quoteGuessed && turnsLeft > 0)
{
Console.Write("Enter your letter ==>");
try
{
userInput = Console.ReadLine().ToUpper();
userGuess = userInput[0];
if (correctGuesses.Contains(userGuess))
{
Console.WriteLine("You've already tried '{0}', and it was correct!", userGuess);
continue;
}
else if (incorrectGuesses.Contains(userGuess))
{
Console.WriteLine("You've already tried '{0}', and it was wrong!", userGuess);
continue;
}
if (quoteToGuessUppercase.Contains(userGuess))
{
correctGuesses.Add(userGuess);
for (int i = 0; i < quoteToGuess.Length; i++)
{
if (quoteToGuessUppercase[i] == userGuess)
{
displayToPlayer[i] = quoteToGuess[i];
lettersRevealed++;
}
}
if (lettersRevealed == quoteToGuess.Length)
{
quoteGuessed = true;
}
turnsLeft--;
Console.Clear();
Console.WriteLine("You have guessed {0} letter(s) out of a total of {1}", lettersRevealed, quoteToGuessLength);
Console.WriteLine("You have {0} attempts remaining!", turnsLeft);
Console.WriteLine();
Console.WriteLine(displayToPlayer.ToString());
Console.WriteLine();
Console.WriteLine("Correct guess, there's a '{0}' in it!", userGuess);
Console.WriteLine();
}
else
{
incorrectGuesses.Add(userGuess);
turnsLeft--;
Console.Clear();
Console.WriteLine("You have guessed {0} letter(s) out of a total of {1}", lettersRevealed, quoteToGuessLength);
Console.WriteLine("You have {0} attempts remaining!", turnsLeft);
Console.WriteLine();
Console.WriteLine(displayToPlayer.ToString());
Console.WriteLine();
Console.WriteLine("Incorrect guess, there's no '{0}' in it!", userGuess);
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("Enter A Valid Character");
}
}
if (quoteGuessed)
{
Console.WriteLine(quoteToGuess);
Console.WriteLine("You have guessed all {0} letters out of a total of {0} Well done!!!", quoteToGuessLength);
}
else
{
Console.WriteLine("You lost! It was '{0}'", quoteToGuess);
}
}
}
}
如果我理解正确的话,这可能会奏效
for (int i = 0; i < quoteToGuess.Length; i++)
{
displayToPlayer.Append(quoteToGuess[i] == ' ' ? ' ' : '*');
}
它的基本意思是,如果quoteToGuess中i的索引处的字符是“”,那么添加“”,如果不是添加“*”
条件运算符?:,通常称为三元运算符
条件运算符,根据值返回两个值中的一个
布尔表达式的。以下是条件语句的语法
接线员
索引是字符对象的位置,而不是Unicode字符在索引中的位置
一串。索引是从零开始的非负数
从字符串中的第一个位置开始,即索引位置零
您可以使用System完全摆脱StringBuilder和for循环。Linq:
如果由于似乎未定义Select而出现错误,请在文件顶部添加以下内容:
using System.Linq;
顺便说一句,不需要使用StringBuilder。Regex.Replace应该可以这样工作:Regex.Replaceinput,[^]1我注意到这个解决方案的一个小问题是它仍然需要用户猜测space@BradleyWilson-如果使用nonSpaceChars=quoteToGuess.Replace、.Length之类的内容;您可以不使用字符获取字符串的长度。您的异常处理在我看来非常糟糕。
using System.Linq;