这是我遇到问题的代码,我试图检索错误猜测数量的值(numbWrong)。为什么我无法检索错误猜测的数量?
...
{
Console.WriteLine("Enter your guess letter using lowercase only.");
char input = Console.ReadLine().ToCharArray()[0];
for (int i = 0; i < myWord.Length; i++)
{
//if the user guessed right, replace the correct dash and display to the user
if (myWord[i] == input)
{
count++; //update the count
a[i] = input; //if guess is correct, dash is replaced by what the user used as input
//show the new dash array mixed with correct guesses
for (int j = 0; j < a.Length; j++)
{
Console.Write(a[j] + " ");
}
else
{
numbWrong += 1;
}
}
Console.WriteLine();
Console.WriteLine("You guessed it right after ");
Console.Write(numbWrong);
Console.Write(" incorrect guesses.");
}
错误:
"Use of unassigned local variable 'numbWrong' "
答案 0 :(得分:3)
您的变量未初始化。将其从循环中取出并将其放在代码中,如下所示。希望有所帮助。编辑:prescot先得到它。但是答案相同。
int numbWrong = 0; //change
for (int i = 0; i < myWord.Length; i++)
{
//if the user guessed right, replace the correct dash and display to the user
if (myWord[i] == input)
{
count++; //update the count
a[i] = input; //if guess is correct, dash is replaced by what the user used as input
//show the new dash array mixed with correct guesses
for (int j = 0; j < a.Length; j++)
{
Console.Write(a[j] + " ");
}
}
else
{
numbWrong += 1;
}
}
答案 1 :(得分:2)
您在哪里定义numbWrong
?在for
循环之前,您应该声明numbWrong
:
int numbWrong = 0;
答案 2 :(得分:1)
这应该可以帮助你,你没有初始化你的变量,一旦初始化你的计算错误猜测的方法是错误的,这是因为你在每个字符的每次迭代中计算它,当它应该被计算一次每次迭代都在一个单词上。
play.cueVideo("VideoId")