好吧,我发现了一个无法访问的代码,而不是所有的代码路径从代码可达代码,而不是所有的代码路径返回一个值
private string MoveForward()
{
//if current direction is east, GetRoomEast
if (Player.CurrentDirection == "EAST")
{
return GetroomEast();
if (Player.NextMove != "")
{
Player.Y++;
}
else
{
Console.WriteLine("You Bumped into a Wall");
}
//if GetRoomEast is not "", then playercol = playercol+1;
//if current direction is west...
}
}
该块返回值,我在上面初始化的变量是
public struct DragonPlayer
{
public int X, Y;
public string CurrentDirection;
public string NextMove;
}
public class DragonGameboard
{
public string[,] GameboardArray;
public DragonPlayer Player;
private Random r;
public DragonGameboard()
{
GameboardArray = new string[4, 4];
Player.CurrentDirection = "EAST";
Player.NextMove = "";
r = new Random();
Player.X = r.Next(0, 4);
Player.Y = r.Next(0, 4);
GenerateRandomBoard();
}
}
为什么这样做?我确定它必须是非常愚蠢的东西,但我很难弄清楚它是什么?
我的意思是我得到这些错误的顶部。 – user3196400
你尝试过研究错误吗?它几乎总结了这一点:您不返回所有代码路径的值,而返回类型为“string”,而“无法访问的代码”意味着该代码将永远不会到达,因为您返回之前。 – CodeCaster
@CodeCaster使用Google的Beucase比要求堆栈溢出更难':)' –