当我已经有一个}时,“期望CS1513}

时间:2019-11-12 10:16:00

标签: c#

我一直在尝试编写此小段代码,并且此CS1513错误不断显示。

我一直在寻找任何流浪的分号,花括号,但找不到。在尝试添加try-catch之前,代码工作正常。 我是C#的新手,所以如果它很明显,请告诉我,因为我什么都看不到。

private static void MakePlayerMove(ref char[,] Board, ref ShipType[] Ships, ref int count)
    {
        int Row = 0, Column = 0, hitCount = 0;
        bool missile = true;
        GetRowColumn(ref Row, ref Column, ref missile);
        if (Board[Row, Column] == 'm' || Board[Row, Column] == 'h')
        {
            Console.WriteLine("Sorry, you have already shot at the square (" + Column + "," + Row + "). Please try again.");
        }
        else if (missile == false)
        {
            if (Board[Row, Column] == '-')
            {
                Console.WriteLine("Sorry, (" + Column + "," + Row + ") is a miss.");
                Board[Row, Column] = 'm';
            }
            else
            {
                Console.WriteLine("Hit at (" + Column + "," + Row + ").");
                Board[Row, Column] = 'h';
            }
        }
        try
        { //This one is causing the problem
            else if (missile == true)
            {
                Row -= 1;
                Column -= 1;
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (Board[Row, Column] != '-')
                        {
                            hitCount += 1;
                        }
                        Column += 1;
                    }
                    Row += 1;
                }
            }
        }
        catch (System.IndexOutOfRangeException)
        {
            Console.WriteLine("Please enter a value that is not on the edge of the board.");
            count += 1;
            throw;
        }
        Console.WriteLine($"You have {29 - count} turns left.");
    }

2 个答案:

答案 0 :(得分:1)

您需要更改

try
{ //This one is causing the problem
   else if (missile == true)
   {
   }
}
catch (System.IndexOutOfRangeException)
{
}

else if (missile == true)
{
   try
   { 
   }
   catch (System.IndexOutOfRangeException)
   {
   }
}

IndexOutOfRangeException

您实际上不必处理IndexOutOfRangeException-使代码不会成为更好的选择。

答案 1 :(得分:0)

很明显首先要面对错误,而实际上这很重要,它们表明您正在尝试。来到你问的问题。

这是有效的声明吗?

private static void MakePlayerMove(ref char[,] Board, ref ShipType[] Ships, ref int count)
{
    //statement
}

我的意思是像在,中一样,一个char数组之间应该有一个ref char[,] Board吗?

如果没有,您能否提及显示的完整错误?