我添加了初始化以显示数组。当它命中else并尝试将输入插入数组时,它会抛出异常。我在网上尝试了几个不同的例子,还没有找到解决方案。我很感激所给予的任何帮助。
private char[] currentMisses;
public int makeTurn()
{
// Ask the user for a guess
Console.Write("Choose A Letter: ");
// Get the input from the user
var currentGuess = Console.ReadKey();
char input = currentGuess.KeyChar;
input = Char.ToUpper(input);
// Do input conversions
while (!char.IsLetter(input))
{
//Console.Write(input);
currentGuess = Console.ReadKey(true);
input = currentGuess.KeyChar;
input = Char.ToUpper(input);
}
// See if it exists in our word
bool test = false;
test = currentWord.Contains(input);
Console.WriteLine("Hello" + input);
// If we didn't find it
if (!test)
{
if (currentMisses != null)
{
Console.WriteLine("WORD");
// Make sure it does not currently exist in the misses array
for (int i = 0; i < currentMisses.Length; i++)
{
if (currentMisses[i] != input)
{
currentMisses[i] = input;
Console.WriteLine("In Loop");
}
Console.WriteLine("Not in Loop");
}
}
else
{
/* This is where I am trying to insert the input from the user to the char array currentMisses, I've tried multiple ways and it seems simple but I hit a roadblock*/
currentMisses[0] = input;
}
答案 0 :(得分:1)
你的逻辑有点偏僻。在你的if语句中,你说&#34;如果我的数组不是null&#34;然后遍历数组else(即你有一个空数组)&#34;尝试插入那个空数组&#34;
您需要使用以下内容初始化数组:
private char[] currentMisses = new char[x]
x
你需要多大的数组。
答案 1 :(得分:1)
我会改变这个:
private char[] currentMisses
到此:
int numberOfWrongGuessesAllowed = 10 //however many guess you allow user to make
private char[] currentMisses = new char[numberOfWrongGuessesAllowed]