C# >我认为这就是问题所在。 char[] partialWord = word.ToCharArray(); int numberOfCharsToHide = word.Length / 2; //divide word
>我认为这就是问题所在。C# >我认为这就是问题所在。 char[] partialWord = word.ToCharArray(); int numberOfCharsToHide = word.Length / 2; //divide word,c#,textbox,C#,Textbox,>我认为这就是问题所在。 char[] partialWord = word.ToCharArray(); int numberOfCharsToHide = word.Length / 2; //divide word length by 2 to get chars to hide Random randomNumberGenerator = new Random(); //generate rand number Hash
char[] partialWord = word.ToCharArray();
int numberOfCharsToHide = word.Length / 2; //divide word length by 2 to get chars to hide
Random randomNumberGenerator = new Random(); //generate rand number
HashSet<int> maskedIndices = new HashSet<int>(); //This is to make sure that I select unique indices to hide. Hashset helps in achieving this
for (int i = 0; i < numberOfCharsToHide; i++) //counter until it reaches words to hide
{
int rIndex = randomNumberGenerator.Next(0, word.Length); //init rindex
while (!maskedIndices.Add(rIndex))
{
rIndex = randomNumberGenerator.Next(0, word.Length); //This is to make sure that I select unique indices to hide. Hashset helps in achieving this
}
partialWord[rIndex] = '_'; //replace with _
}
return new string(partialWord);
var builder = new StringBuilder(word);
for (int i = 0 ; i < word.Length ; i++) {
builder.Insert(i * 2, " ");
}
return builder.ToString().TrimStart(' '); // TrimStart is called here to remove the leading whitespace. If you want to keep it, delete the call.
// char[] partialWord is used from question code
char[] result = new char[(partialWord.Length * 2) - 1];
for(int i = 0; i < result.Length; i++)
{
result[i] = i % 2 == 0 ? partialWord[i / 2] : ' ';
}
return new string(result);